variable-declaration

Golang mixed assignation and declaration

两盒软妹~` 提交于 2019-11-27 23:36:15
I started working with go for a few weeks, and (once again) I stumbled across something that seems odd for me: // Not working a := 1 { a, b := 2, 3 } // Works a := 1 a, b := 2, 3 playground I want to assign two variables simultaneously. One is already declared, in a superior scope, the other one is not. It does not work: the compiler tries to redeclare the former variable. However, it works fine if this variable is declared in the same scope. Why is that ? What you're experiencing is commonly known as "variable shadowing" . When you use := with any variable in an inner scope, including in

Possible to initialize an array after the declaration in C?

好久不见. 提交于 2019-11-27 21:53:26
Is there a way to declare a variable like this before actually initializing it? CGFloat components[8] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.15 }; I'd like it declared something like this (except this doesn't work): CGFloat components[8]; components[8] = { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.15 }; You cannot assign to arrays so basically you cannot do what you propose but in C99 you can do this: CGFloat *components; components = (CGFloat [8]) { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.15 }; the ( ){ } operator is called the compound literal operator. It is a C99 feature. Note that in this

Variable declaration after goto Label

拟墨画扇 提交于 2019-11-27 17:56:27
Today I found one interesting thing. I didn't know that one can't declare a variable after a goto label. Compiling the following code #include <stdio.h> int main() { int x = 5; goto JUMP; printf("x is : %d\n",x); JUMP: int a = 0; <=== giving me all sorts of error.. printf("%d",a); } gives errors like temp.c: In function ‘main’: temp.c:7: error: expected expression before ‘int’ temp.c:8: error: ‘a’ undeclared (first use in this function) temp.c:8: error: (Each undeclared identifier is reported only once temp.c:8: error: for each function it appears in.) Now what is the logic behind that? I

Declaring javascript variables as specific types [closed]

喜你入骨 提交于 2019-11-27 17:16:34
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 6 years ago . The title says it all, but I will provide more clarification: After seeing many samples of javascript where all variables are declared

C++11 - declaring non-static data members as 'auto'

扶醉桌前 提交于 2019-11-27 12:01:17
Does C++11 allow declaring non-static data members as 'auto' if they are initialized in the declaration? For example: struct S { auto x = 5; // in place of 'int x = 5;', which is definitely allowed }; GCC 4.7 rejects the above code, while it accepts int x = 5; . Assuming this is not a compiler bug but rather the standard really doesn't allow it, why not? It would be just as useful as declaring local variables auto . The rule for prohibiting non-static members is in 7.1.6.4 clause 4: The auto type-specifier can also be used in declaring a variable in the condition of a selection statement (6.4)

Can I simultaneously declare and assign a variable in VBA?

余生颓废 提交于 2019-11-27 11:59:26
I'm new to VBA and want to know if I can convert the following declaration and assignment into one line: Dim clientToTest As String clientToTest = clientsToTest(i) or Dim clientString As Variant clientString = Split(clientToTest) Alex K. There is no shorthand in VBA unfortunately, The closest you will get is a purely visual thing using the : continuation character if you want it on one line for readability; Dim clientToTest As String: clientToTest = clientsToTest(i) Dim clientString As Variant: clientString = Split(clientToTest) Hint (summary of other answers/comments): Works with objects too

C error: Expected expression before int

纵然是瞬间 提交于 2019-11-27 10:19:19
问题 When I tried the following code I get the error mentioned. if(a==1) int b =10; But the following is syntactically correct if(a==1) { int b = 10; } Why is this? 回答1: This is actually a fairly interesting question. It's not as simple as it looks at first. For reference, I'm going to be basing this off of the latest C11 language grammar defined in N1570 I guess the counter-intuitive part of the question is: if this is correct C: if (a == 1) { int b = 10; } then why is this not also correct C? if

What is the purpose of a declaration like int (x); or int (x) = 10;

冷暖自知 提交于 2019-11-27 08:33:47
If you look at the grammar for *declarator*s in §8/4 you'll notice that a noptr-declarator can be written as ( ptr-declarator ), that is, it can be written as ( declarator-id ), which validates declarations like the ones in the title. As matter of fact this code compiles without a problem: #include <iostream> struct A{ int i;}; int (x) = 100; A (a) = {2}; int main() { std::cout << x << '\n'; std::cout << a.i << '\n'; } But what is the purpose of allowing these parentheses when a pointer (to an array or to a function) is not involved in the declaration? The fact that this rule is applicable in

Python - Difference between variable value declaration on Fibonacci function

南笙酒味 提交于 2019-11-27 08:11:53
问题 I'm kind of beginner in python. I was looking at one the types to make a fibonacci function, def fib(n): a=0 b=1 while a<n: print a a,b=b,a+b and I saw the a,b=b,a+b declaration. So, I thought a=b and b=a+b were the same to a,b=a,b+a, so I changed the function for it to be like this: def fib(n): a=0 b=1 while a<n: print a a=b b=a+b and I thought it would be right, but when I executed the program, I got a different output. Can someone explain to me the difference between those two types of

Java switch : variable declaration and scope

雨燕双飞 提交于 2019-11-27 06:57:04
问题 How does the Java compiler handle the following switch block ? What is the scope of the 'b' variable ? Note that the 'b' variable is declared only in the first branch of the switch statement. Attempting to declare it in the second branch as well results in a "duplicate local variable" compilation error. int a = 3; switch( a ) { case 0: int b = 1; System.out.println("case 0: b = " + b); break; case 1: // the following line does not compile: b may not have been initialized // System.out.println