variable-declaration

Array declaration in Fortran

别来无恙 提交于 2019-11-26 21:53:38
问题 Consider INTEGER,DIMENSION(3) :: NumberVector and INTEGER :: NumberVector(3) Is there any difference whatsoever between these two declarations or are they exactly the same? (I mean in ANY possible context and variation: for example, in the case that those two were identical, what if I am declaring an array with an implicit size as one of the input parameter of a subroutine? Would it still be irrelevant which one I used?) 回答1: Yes, it is identical. Even for assumed, deferred and whatever

Possible to initialize an array after the declaration in C?

喜你入骨 提交于 2019-11-26 20:48:54
问题 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 }; 回答1: 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

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

社会主义新天地 提交于 2019-11-26 17:45:58
问题 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

Golang mixed assignment and declaration

折月煮酒 提交于 2019-11-26 16:59:31
问题 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? 回答1: What you're experiencing is

C++, variable declaration in 'if' expression

橙三吉。 提交于 2019-11-26 16:04:29
What's going on here? if(int a = Func1()) { // Works. } if((int a = Func1())) { // Fails to compile. } if((int a = Func1()) && (int b = Func2())) ) { // Do stuff with a and b. // This is what I'd really like to be able to do. } Section 6.4.3 in the 2003 standard expains how variables declared in a selection statement condition have scope that extends to the end of the substatements controlled by the condition. But I don't see where it says anything about not being able to put parenthesis around the declaration, nor does it say anything about only one declaration per condition. This limitation

C++11 - declaring non-static data members as &#39;auto&#39;

人走茶凉 提交于 2019-11-26 15:51:52
问题 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 . 回答1: The rule for prohibiting non-static members is in 7.1.6.4 clause 4: The

Can I declare variables of different types in the initialization of a for loop? [duplicate]

浪尽此生 提交于 2019-11-26 11:13:13
问题 This question already has answers here : Is it possible to declare two variables of different types in a for loop? (8 answers) Closed 3 years ago . Why does this C++ code not compile under VS2010: for ( int a = 0, short b = 0; a < 10; ++a, ++b ) {} while this one does: short b = 0; for ( int a = 0; a < 10; ++a, ++b ) {} Is the declaration of two variables of different types inside the for-loop initializer prohibited? If so, how can you work around it? 回答1: Yes, that is prohibited. Just as

Why is initialization of a new variable by itself valid? [duplicate]

元气小坏坏 提交于 2019-11-26 10:00:02
问题 This question already has answers here : What's the behavior of an uninitialized variable used as its own initializer? (3 answers) Closed 10 months ago . Consider some code: #include <iostream> int main() { using std::cout; int a=3; cout << \"a=\"<<a<<\"\\n\"; { int a=a; cout << \"new a = \" << a << \"\\n\"; a=5; cout << \"a = \" << a << \"\\n\"; } cout << \"old a = \" << a << \"\\n\"; } I\'d expect it to print a=3 new a = 3 changed a = 5 old a = 3 But what I get actually appears to say new a

Declaring variables inside loops, good practice or bad practice?

丶灬走出姿态 提交于 2019-11-26 09:28:11
Question #1: Is declaring a variable inside a loop a good practice or bad practice? I've read the other threads about whether or not there is a performance issue (most said no), and that you should always declare variables as close to where they are going to be used. What I'm wondering is whether or not this should be avoided or if it's actually preferred. Example: for(int counter = 0; counter <= 10; counter++) { string someString = "testing"; cout << someString; } Question #2: Do most compilers realize that the variable has already been declared and just skip that portion, or does it actually

setq and defvar in Lisp

被刻印的时光 ゝ 提交于 2019-11-26 09:22:30
问题 I see that the Practical Common Lisp uses (defvar *db* nil) for setting up a global variable . Isn\'t it OK to use setq for the same purpose? What are the advantages/disadvantages of using defvar vs. setq ? 回答1: There are several ways to introduce variables. DEFVAR and DEFPARAMETER introduce global dynamic variables. DEFVAR optionally sets it to some value, unless it is already defined. DEFPARAMETER sets it always to the provided value. SETQ does not introduce a variable. (defparameter