关于一个时髦的数组声明的问题
我刚刚遇到这个数组声明:
const int nNums= 4;
int* nums[nNums] = {0, 0, 0}, d[nNums];
我知道正在创建一个指向 nums 的指针,但是右边的业务是什么? d[]
已初始化,但我不太确定 {0,0,0}
的作用。
I just came across this array declaration:
const 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
int* nums[nNums] = {0, 0, 0}
定义了一个由 4 个整数指针组成的数组,每个指针都初始化为 NULL。但是,请注意,d
是整数 和非 整数指针的数组,并且这些值未初始化。int* nums[nNums] = {0, 0, 0}
defined a array of 4 integer pointers each initialized to NULL. However, note thatd
is an array of integers and not integer pointers and these values are not initialized.该代码相当于:
因此,
nums
是一个长度为 4 的int*
数组,所有四个元素都初始化为 null;d
是长度为 4 的int
数组,所有四个元素都未初始化(再次强调,d
确实不以任何方式初始化)。此上下文中的语法
= {0, 0, 0}
称为“聚合初始化”,在 C++03 标准的第 8.5.1 节中进行了描述;本守则的相关部分 (§8.5.1/2) 规定:因此,
nums
的前三个元素显式初始化为0
,第四个元素隐式“值初始化”,如 §8.5.1/7 中所述:值初始化在 §8.5/5 中描述:
这会导致
nums
的第四个元素也被初始化为 null。That code is equivalent to:
So,
nums
is an array ofint*
s of length 4, with all four elements initialized to null;d
is an array ofint
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:So, the first three elements of
nums
are explicitly initialized to0
, and the fourth element is implicitly "value-initialized", as stated in §8.5.1/7:Value-initialization is described in §8.5/5:
This results in the fourth element of
nums
also being initialized to null.正如 @Asha 已经说过的那样,nums 是一个由 4 个整数指针组成的数组,每个指针都初始化为 NULL。
这里可以进一步问的有趣问题是:变量 d 的类型是什么?
或者
所以答案是:它是一个由 4 个整数组成的数组。
*
仅与第一个声明的符号nums
关联。等效的声明是这样的:
为了避免这种混乱,我更喜欢将这样的声明写在多行上。如果你想在一行中声明,第二个声明会更好一些:
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
?Or
So the answer is : its an array of 4 integers. The
*
is associated with only first declared symbolnums
.The equivalent declaration would be this:
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:
你理解错误。该声明中没有创建“指向 nums 的指针”。声明
声明了一个包含 4 个指针的数组。
nums
本身是一个数组,而不是指向任何东西的指针。= {0, 0, 0}
部分称为“聚合初始值设定项”。它初始化 nums 数组的第一个树元素。目前尚不清楚为什么只有三个被显式初始化(而第四个则被隐式初始化为零)。此外,在 C++ 中,可以通过声明实现相同的效果,其中所有四个元素都隐式初始化为零。
啊?不会。
d
的声明相当于d
根本没有被初始化。You understand incorrectly. There's no "pointer to
nums
" being created in that declaration. Declarationdeclares an array of 4 pointers.
nums
itself is an array, not a pointer to anything.The
= {0, 0, 0}
parts is called "aggregate initializer". It initializes the first tree elements of thenums
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 adeclaration, where all four elements are initialized to zero implicitly.
Huh? No. The declaration of
d
is equivalent tomeaning that
d
does not get initialized at all.