结构体中的长度可变数组。

#include <stdio.h>

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


struct block {
  int length;
  char contents[];  /* xx[] is at the end of the struct only. */
//  int text[20]; /* contexts[]只能在结构体的最尾处。 */
};


int main()
{
    struct line *thisline = (struct line *)malloc(sizeof(struct line) + 10);
    thisline->length = 10;
   
    struct block *thisblock = (struct block *)malloc(sizeof(struct block) + 10);
    thisblock->length = 10;
    printf("sizeof(struct line) = %d/n",sizeof(struct line));  /* 打印结果是4. */
    printf("sizeof(struct block) = %d/n",sizeof(struct block));
}
---------------------------------------------------------------------------------------

liudan@liudan-desktop:/media/LIUDAN/aa/array_len_zero$
liudan@liudan-desktop:/media/LIUDAN/aa/array_len_zero$ ./arr
sizeof(struct line) = 4
sizeof(struct block) = 4
liudan@liudan-desktop:/media/LIUDAN/aa/array_len_zero$

你可能感兴趣的:(struct,include)