关于一个时髦的数组声明的问题

发布于 2024-10-31 19:37:59 字数 201 浏览 6 评论 0原文

我刚刚遇到这个数组声明:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

锦上情书 2024-11-07 19:37:59

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 that d is an array of integers and not integer pointers and these values are not initialized.

长亭外,古道边 2024-11-07 19:37:59

该代码相当于:

const int nNums= 4;
int* nums[nNums] = {0, 0, 0};
int d[nNums];

因此,nums 是一个长度为 4 的 int* 数组,所有四个元素都初始化为 null; d 是长度为 4 的 int 数组,所有四个元素都未初始化(再次强调,d 确实以任何方式初始化)。

此上下文中的语法 = {0, 0, 0} 称为“聚合初始化”,在 C++03 标准的第 8.5.1 节中进行了描述;本守则的相关部分 (§8.5.1/2) 规定:

当聚合初始化时,初始化器可以包含一个初始化器子句,由大括号括起来、以逗号分隔的初始化器子句列表组成em> 表示聚合的成员,以递增的下标或成员顺序书写。如果聚合包含子聚合,则此规则递归地应用于子聚合的成员。

因此,nums 的前三个元素显式初始化为 0,第四个元素隐式“值初始化”,如 §8.5.1/7 中所述:

如果列表中的初始化器数量少于聚合中的成员数量,则每个未显式初始化的成员都应进行值初始化。

值初始化在 §8.5/5 中描述:

T 类型的对象进行值初始化意味着:

  • 如果 T 是具有用户声明的构造函数的类类型,则调用 T 的默认构造函数(如果 T 没有可访问的默认构造函数);
  • 如果 T 是没有用户声明的构造函数的非联合类类型,则 T 的每个非静态数据成员和基类组件都是值-已初始化;
  • 如果T是数组类型,则每个元素都是值初始化的;
  • 否则,对象将被零初始化

T 类型的对象进行零初始化意味着:

  • 如果 T 是标量类型,则将对象设置为转换为 T0(零)值;
  • 如果T是非联合类类型,则每个非静态数据成员和每个基类子对象都被零初始化;
  • 如果T是联合类型,则对象的第一个命名数据成员被零初始化;
  • 如果T是数组类型,则每个元素都初始化为零;
  • 如果 T 是引用类型,则不执行初始化。

这会导致 nums 的第四个元素也被初始化为 null。

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 ints 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 for T is called (and the initialization is ill-formed if T 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 of T 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 of 0 (zero) converted to T;
  • 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.

南冥有猫 2024-11-07 19:37:59
int* nums[nNums] = {0, 0, 0}, d[nNums];

正如 @Asha 已经说过的那样,nums 是一个由 4 个整数指针组成的数组,每个指针都初始化为 NULL。

这里可以进一步问的有趣问题是:变量 d 的类型是什么?

它是一个由 4 个整数指针组成的数组吗?

或者

它是一个由 4 个整数组成的数组吗?

所以答案是:它是一个由 4 个整数组成的数组。 * 仅与第一个声明的符号 nums 关联。

等效的声明是这样的:

int* nums[nNums] = {0, 0, 0};
int d[nNums]; //not int* d[nNums];

为了避免这种混乱,我更喜欢将这样的声明写在多行上。如果你想在一行中声明,第二个声明会更好一些:

int*  nums[nNums] = {0, 0, 0}, d[nNums];  //old one!
int  *nums[nNums] = {0, 0, 0}, d[nNums];  //new one. note the position of *
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 *
隐诗 2024-11-07 19:37:59

>我知道指向 nums 的指针是
正在创建,

你理解错误。该声明中没有创建“指向 nums 的指针”。声明

int* nums[4] = {0, 0, 0};

声明了一个包含 4 个指针的数组。 nums 本身是一个数组,而不是指向任何东西的指针。

>但右边的生意是什么?

= {0, 0, 0} 部分称为“聚合初始值设定项”。它初始化 nums 数组的第一个树元素。目前尚不清楚为什么只有三个被显式初始化(而第四个则被隐式初始化为零)。此外,在 C++ 中,可以通过声明实现相同的效果

int* nums[4] = {};

,其中所有四个元素都隐式初始化为零。

> d[] 被初始化

啊?不会。d 的声明相当于

int d[4];

d 根本没有被初始化。

> 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文