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)?

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章