This is a very basic thing, but the importance of the foundation is self-evident, I am sure I have known this knowledge, but now, I am not sure, this shows the importance of the record, the world there is no shortcut to find the right direction, and then repeated. So from today, I will be more detailed record of these relatively small knowledge points, In fact, there are a lot of interesting places.
The reason for writing this article is <<com Technology Insider >> Seventh chapter new things too much, see I dizzying, so look for some examples on the internet, which has an example of this statement: ...
wchar_t wname[128] = ... {0};
Char cname[256] = ... {0};
...
What I am interested in is:
1. The result of this assignment.
2. Does this form conform to the standard coding rules?
I have found the following information that may be helpful in mastering this knowledge point. /**/ /*
The number of initialization values can be less than the number of array elements. When the number of initialization values is less than the number of array elements, the preceding sequence initializes the corresponding value, followed by an initialization of 0 (global or static array) or an indeterminate value (a local array).
*/
I believe that the above information is the C and C + + language standard specification, however, when the actual compiler is processed, it may differ from the specification. Because the compiler is in principle to comply with the language specification, but for the local array of the value of the uncertainty is how much, how to deal with, the compiler can be flexible. I tested three compilers, In fact, the compiler gives the value is fixed, is 0.
In this blog http://hi.baidu.com/widebright/blog/item/a024bc09631402256b60fbd0.html talked about the same topic, the excerpt is as follows:/**//*
Always thought int a[256]={0}; Initializes all elements of A to 0,int a[256]={1}; Initializes all elements of a to 1.
Viewing memory when you are debugging it's not that much of a thing, it turns out "the C + + programming Language" finally has a verdict. The PDF is not even copied, it is translated into this chapter, as follows
5.2.1 Array Initialization
Arrays can be initialized with a column value, such as
int v1[] ={1,2,3,4};
Char v2[]={' A ', ' B ', ' C ', 0};
When the array definition does not specify a size, when initialization takes the list initialization, the size of the array is determined by the number of list elements initialized. So v1 and v2 respectively are int[4] and char[4] types. If the array size is specified explicitly, an error is generated when the number of elements specified in the initialization exceeds this size. For example:
Char v3[2] ={' A ', ' B ', 0}; Error: Too many initialization values
Char v3[3] ={' A ', ' B ', 0}; That's right
If the number of elements specified when initializing is less than the size of the array, the remaining elements are initialized to 0. For example
int v5[8]={1,2,3,4};
Equivalent to
int v5[8]={1,2,3,4,0,0,0,0};
Note that there is no array assignment in the following form:
void F ()
{
v4={' C ', ' d ', 0}; Error: Not array assignment
}
If you want to replicate this, use vector (Section 16, sect. III) or Valarray (section Fourth of Chapter 22).
Character arrays can be easily initialized with strings (refer to Chapter fifth 2.2)
Yes, that's it. Char Alpha []= "ABCDEFGHIJKLMN";
*/
Here's an example: #include < iostream.h >
int array1[5] = ... {1,2,3};
static int array2[5] = ... {1};
void Main ()
... {
int arr1[5]= ... {2};
static int arr2[5]= ... {1,2};
int n;
cout << "Global:";
for (n=0; n<5; n++)
cout << "" <<array1[n];
cout << "global Static:";
for (n=0; n<5; n++)
cout << "" <<array2[n];
cout << "Local:";
for (n=0; n<5; n++)
cout << "" <<arr1[n];
cout << "local static:";
for (n=0; n<5; n++)
cout << "" <<arr2[n];
cout <<endl;
}
In this example, both global and static arrays are initialized to 0 according to the language specification requirements, but the local array is not as indeterminate as previously said, and the following is the result of compiling separately with gcc,vc6.0,tuborc++ (note that GCC compiles C + + files with g++ and GCC does not link libraries):