# include "stdafx. H"
#include
using namespace std;
Int a [3]={1, 2, 3};
Void main ()
{
Cout & lt;}
# include "stdafx. H"
#include
using namespace std;
Int a, [3].
Void main ()
{
A [3]={1, 2, 3};
Cout & lt;}
Would you please tell me why what is the difference between the two groups of code, the second group will go wrong
Compile the given error as shown in figure
Can't array assignment, (please don't give an example of a for loop, 1 2 3 just write here, not necessarily regular) also can be directly output array a, why the first set of code compiled by the display of the result is that
CodePudding user response:
int a [3]={1, 2, 3};the [3] said the number of array;
a [3]={1, 2, 3};the subscript 3 [3]
CodePudding user response:
The first way:# include "stdafx. H"
#include
using namespace std;
Int a [3]={1, 2, 3};
Void main ()
{
for (int i=0; I & lt; 3; I++)
Cout & lt; }
The second way:
# include "stdafx. H"
#include
using namespace std;
Int a, [3].
Void main ()
{
for (int i=0; I & lt; 3; I++)
A [I]=I + 1;
for (int i=0; I & lt; 3; I++)
Cout & lt; }
CodePudding user response:
"Variable assignment" and "variable initialization" is not the same thing!"Variable assignment" occur at runtime, grammar rules, their wording follow assignment
"Variable initialization" occur at compile time or run time, the writing follows initialization list syntax rules,
CodePudding user response:
Int a [3] is to define an array, a [3] is a [3] this element, what's the difference between what you saidCodePudding user response:
Play soy sauce, floats throughCodePudding user response:
Want to want to use a for loop output arrayfor (int i=0; I & lt; 3; + + I)
Cout & lt; CodePudding user response: Array initialization only can use braces way of grammar, the second piece of code you can't use, only one element value assigned an element
CodePudding user response:
Ha ha, I also made this low-level mistakesCodePudding user response:
The first paragraph is a first address values of the array output, the equivalent of & amp; A [0] value.