在GNU C中允许声明零长度数组作为扩展。零长度数组可以用作结构的最后一个元素,该数组实际上是可变长度对象的标头。
#include <stdio.h> int main() { struct line { int length; char contents[0]; }; int this_length = 3; struct line *thisline = (struct line *) malloc(sizeof(struct line) + this_length); thisline->length = this_length; thisline->contents[0] = 1; thisline->contents[1] = 3; thisline->contents[2] = 9; for (int i = 0; i < thisline->length; ++i) { printf("thisline->contents[%d]:%d\n",i,thisline->contents[i]); } return 0; }