Question about a funky array declaration
I just came across this array declaration:
cons开发者_如何学运维t int nNums= 4;
int* nums[nNums] = {0, 0, 0}, d[nNums];
I understand that a pointer to nums
is being created, but what is the business on the right? d[]
gets initialized, but I am not quite sure what the {0,0,0}
does.
int* nums[nNums] = {0, 0, 0}
defined a array of 4 integer pointers each initialized to NULL. However, note that d
is an array of integers and not integer pointers and these values are not initialized.
That code is equivalent to:
const int nNums= 4;
int* nums[nNums] = {0, 0, 0};
int d[nNums];
So, nums
is an array of int*
s of length 4, with all four elements initialized to null; d
is an array of int
s of length 4, with all four elements uninitialized (to reemphasize, d
does not get initialized in any way).
The syntax = {0, 0, 0}
in this context is known as "aggregate initialization", and is described in §8.5.1 of the C++03 standard; the relevant portion for this code (§8.5.1/2) states:
When an aggregate is initialized the initializer can contain an initializer-clause consisting of a brace-enclosed, comma-separated list of initializer-clauses for the members of the aggregate, written in increasing subscript or member order. If the aggregate contains subaggregates, this rule applies recursively to the members of the subaggregate.
So, the first three elements of nums
are explicitly initialized to 0
, and the fourth element is implicitly "value-initialized", as stated in §8.5.1/7:
If there are fewer initializers in the list than there are members in the aggregate, then each member not explicitly initialized shall be value-initialized.
Value-initialization is described in §8.5/5:
To value-initialize an object of type
T
means:
- if
T
is a class type with a user-declared constructor, then the default constructor forT
is called (and the initialization is ill-formed ifT
has no accessible default constructor);- if
T
is a non-union class type without a user-declared constructor, then every non-static data member and base-class component ofT
is value-initialized;- if
T
is an array type, then each element is value-initialized;- otherwise, the object is zero-initialized
To zero-initialize an object of type
T
means:
- if
T
is a scalar type, the object is set to the value of0
(zero) converted toT
;- if
T
is a non-union class type, each nonstatic data member and each base-class subobject is zero-initialized;- if
T
is a union type, the object’s first named data member) is zero-initialized;- if
T
is an array type, each element is zero-initialized;- if
T
is a reference type, no initialization is performed.
This results in the fourth element of nums
also being initialized to null.
int* nums[nNums] = {0, 0, 0}, d[nNums];
As @Asha already said that nums
is an array of 4 integer pointers each initialized to NULL.
The interesting question which can be further asked here is : what is the type of the variable d
?
Is it an array of 4 integer pointers?
Or
Is it an array of 4 integers?
So the answer is : its an array of 4 integers. The *
is associated with only first declared symbol nums
.
The equivalent declaration would be this:
int* nums[nNums] = {0, 0, 0};
int d[nNums]; //not int* d[nNums];
To avoid such confusion, I prefer to write such declaration on multiple lines. If you want to declare in one line, the second declaration would be a bit better:
int* nums[nNums] = {0, 0, 0}, d[nNums]; //old one!
int *nums[nNums] = {0, 0, 0}, d[nNums]; //new one. note the position of *
> I understand that a pointer to nums is being created,
You understand incorrectly. There's no "pointer to nums
" being created in that declaration. Declaration
int* nums[4] = {0, 0, 0};
declares an array of 4 pointers. nums
itself is an array, not a pointer to anything.
> but what is the business on the right?
The = {0, 0, 0}
parts is called "aggregate initializer". It initializes the first tree elements of the nums
array. It is not clear why only three are explicitly initialized (while the fourth one is left to be initialized to zero implicitly). Also, in C++ the same effect can be achieved by a
int* nums[4] = {};
declaration, where all four elements are initialized to zero implicitly.
> d[] gets initialized
Huh? No. The declaration of d
is equivalent to
int d[4];
meaning that d
does not get initialized at all.
精彩评论