声明"int t [0];"的目的是什么?服务?

elh*_*ɥןǝ 4 c c99

以下声明的目的是什么?

struct test
{
     int field1;
     int field2[0];
};
Run Code Online (Sandbox Code Playgroud)

Tud*_*dor 7

那只是一个0长度的数组.根据http://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html:

在GNU C中允许零长度数组.它们作为结构的最后一个元素非常有用,它实际上是一个可变长度对象的头:

 struct line {
   int length;
   char contents[0];
 };

 struct line *thisline = (struct line *)
 malloc (sizeof (struct line) + this_length);
 thisline->length = this_length;
Run Code Online (Sandbox Code Playgroud)

  • 我知道你从GNU站点复制了这个,所以这不是你的错,但是`this_length`没有在任何地方定义. (3认同)