c struct with array of zero length (1 length or empty length in C99)

details see stackoverflow.

/* C99 */
struct s {
    int n;
    double d[];
};

int m = /* some value */;
struct s *p = malloc(sizeof (struct s) + sizeof (double [m]));


/* before C99 */
 struct bts_action {
         u16 type;
         u16 size;
         u8 data[0];
 };

This is a way to have variable sizes of data, without having to call malloc (kmalloc in this case) twice.

The so-called 柔性数组(flexible array)?

发布了34 篇原创文章 · 获赞 0 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/hfyinsdu/article/details/105155519