variable-assignment

What exactly are C++ definitions, declarations and assignments?

拟墨画扇 提交于 2019-11-27 08:55:35
I tend to use the words define, declare and assign interchangeably but this seems to cause offense to some people. Is this justified? Should I only use the word declare for the first time I assign to a variable? Or is there more to it than that? Define and declare are similar but assign is very different. Here I am declaring (or defining) a variable: int x; Here I am assigning a value to that variable: x = 0; Here I am doing both in one statement: int x = 0; Note Not all languages support declaration and assignment in one statement: T-SQL declare x int; set x = 0; Some languages require that

Can you declare multiple variables at once in Go?

前提是你 提交于 2019-11-27 08:48:27
Is it possible to declare multiple variables at once using Golang? For example in Python you can type this: a = b = c = 80 and all values will be 80. Yes, you can: var a, b, c string a = "foo" fmt.Println(a) You can do something sort of similar for inline assignment, but not quite as convenient: a, b, c := 80, 80, 80 VonC In terms of language specification, this is because the variables are defined with: VarDecl = "var" ( VarSpec | "(" { VarSpec ";" } ")" ) . VarSpec = IdentifierList ( Type [ "=" ExpressionList ] | "=" ExpressionList ) . (From " Variable declaration ") A list of identifiers

C array declaration and assignment?

送分小仙女□ 提交于 2019-11-27 08:24:03
I've asked a similar question on structs here but I'm trying to figure out how C handles things like assigning variables and why it isn't allowed to assign them to eachother if they are functionally the same. Lets say I have two arrays: int x[10]; int y[10]; Why won't x = y compile? If they are both the same "signature" like that, then shouldn't you be able to assign them back and forth? Can I declare these in a way that would allow me to do that in C? It makes sense to me that you would be able to, but maybe there is a way that this can be done? Typedefs for structs seemed to be the solution,

memcpy vs assignment in C

白昼怎懂夜的黑 提交于 2019-11-27 08:17:13
Under what circumstances should I expect memcpys to outperform assignments on modern INTEL/AMD hardware? I am using GCC 4.2.x on a 32 bit Intel platform (but am interested in 64 bit as well). You should never expect them outperform assignments. The reason is, the compiler will use memcpy anyway when it thinks it would be faster (if you use optimize flags). If not and if the structure is reasonable small that it fits into registers, direct register manipulation could be used which wouldn't require any memory access at all. GCC has special block-move patterns internally that figure out when to

Python multiple assignment issue (list) [duplicate]

大兔子大兔子 提交于 2019-11-27 08:14:20
问题 This question already has answers here : Tuple unpacking order changes values assigned (4 answers) Closed 3 years ago . >>> i = 1 >>> A = [3,4,-1,1] >>> A[A[i] - 1], A[i] = A[i], A[A[i] - 1] >>> A [3, 1, -1, 4] >>> A = [3,4,-1,1] >>> A[i], A[A[i] - 1] = A[A[i] - 1], A[i] >>> A [4, 1, -1, 1] I have question when I do assignment for multiple variables for a list. Like the example above, the assignment A[A[i] - 1], A[i] = A[i], A[A[i] - 1] is different from the assignment A[i], A[A[i] - 1] = A[A

In JavaScript, is chained assignment okay?

浪子不回头ぞ 提交于 2019-11-27 07:47:54
Am not new to JS or its syntax, but sometimes, the semantics of the language has me stumped at times. At work today, a colleague mentioned this: var a = b = []; is not the same as var a = [], b = []; or var a = []; var b = []; since the first version actually assigns the reference to an empty array to a and b. I couldn't quite accept this as true, but I'm not sure. What do you all think? Yes, they're not the same. var a = b = [] is equivalent to var a; b = []; a = b; Not only do both a and b get assigned the same value (a reference to the same empty array), b is not declared at all. In strict

Mass variable declaration and assignment in R?

大兔子大兔子 提交于 2019-11-27 07:42:47
问题 Apologies if this is a stupid question - but this my first attempt at using R , and i have ended up writting some code along the lines of: some <- vector('list', length(files)) thing <- vector('list', length(files)) and <- vector('list', length(files)) another <- vector('list', length(files)) thing <- vector('list', length(files)) Is there a nicer (DRY) way to do this in R ? To rephrase, I want to assign the same value to multiple variables at once (as per @Sven Hohenstein's answer) 回答1: If

An unset C pointer is not null

徘徊边缘 提交于 2019-11-27 07:24:19
问题 I am messing around with C pointers. When I compile and run the following code. Example 1: #include <stdio.h> int main() { int k; int *ptr; k = 555; if (ptr == NULL) { printf("ptr is NULL\n"); } else { printf("ptr is not NULL\n"); printf("ptr value is %d\n", *ptr); } printf("ptr address is %p\n", ptr); } I get the output: ptr is not NULL ptr value is 1 ptr address is 0x7fff801ace30 If I don't assign a value to k: Example 2: #include <stdio.h> int main() { int k; int *ptr; if (ptr == NULL) {

Fixing the 'Use of unassigned local variable' with a null assignment. Why?

▼魔方 西西 提交于 2019-11-27 06:33:47
问题 With a piece of code like this, the compiler complains on c.MyProperty : MyClass c; try { throw new Exception(); } catch (Exception) { } c.MyProperty = 2; // "Use of unassigned local variable 'c'". Yet it doesn't complain if you assign a null to c in initialization: MyClass c = null; try { throw new Exception(); } catch (Exception) { } c.MyProperty = 2; // no complains this time. So, why does this work? If c wasn't assigned a null and the compiler hypothetically allowed it, wouldn't the same

uninitialized local variable 'j' used

余生长醉 提交于 2019-11-27 06:33:05
问题 Here is a section of some code I have. Im getting an error uninitialized local variable 'j' used and I dont see it. as far as I can tell it is being used. Can someone please help? float Calculate(Element ElmAry[30], Formula FormAry[30]) { int i; int j; float MoleWT = 0; float MoleSum = 0; char e1; char e2; char f1; char f2; for(i = 0; i < 30; i++) { f1 = FormAry[j].Element1; f2 = FormAry[j].ElementA; e1 = ElmAry[i].eN1; e2 = ElmAry[i].eN1; if(e1 == f1 && e2 == f2) { MoleWT = ElmAry[i].Weight