初始化没有大小的数组字面量

Initialize an Array Literal Without a Size

我对下面的表达式很好奇:

int ints[] = { 1, 2, 3 };

即使在带有 clang 的 c89 环境中,这似乎也可以正常编译。有关于这个的文档吗?在搜索它时,我似乎无法弄清楚要使用的正确术语(我宁愿不再通读整个 c89 规范)。

这是扩展吗?编译器是否只是推断数组的大小?

编辑:我只记得你们喜欢实际编译的代码块,所以这里是:

/* clang tst.c -o tst -Wall -Wextra -Werror -std=c89 */
int main(int argc, const char *argv[]) {
  int ints[] = { 1, 2, 3 }; 
  (void)(ints); (void)(argc); (void)(argv);
  return 0; 
}

它是标准 C 的一部分,因为 C89:

§3.5.7 Initialization

If an array of unknown size is initialized, its size is determined by the number of initializers provided for its members. At the end of its initializer list, the array no longer has incomplete type.

其实还有一个几乎一模一样的例子:

Example:

The declaration

int x[] = { 1, 3, 5 };

defines and initializes x as a one-dimensional array object that has three members, as no size was specified and there are three initializers.

Is this an extension?

不,这是标准,适用于所有版本的 C 标准

=数组类型为"incomplete"然后通过初始化完成

Is the compiler simply inferring the size of the array?