Why does static initialization of flexible array member work?

后端 未结 1 1791
无人共我
无人共我 2020-12-05 14:58

I have written the following basic code for a menu:

typedef struct Menu {
    char* title;
    unsigned num_submenus;
    struct Menu *submenu[];
} Menu;

Me         


        
相关标签:
1条回答
  • 2020-12-05 15:26

    Initialization of flexible array member in this way is not allowed as per C standard.

    C11: 6.7.2.1 Structure and union specifiers (p20-21):

    21 EXAMPLE 2 After the declaration:

    struct s { int n; double d[]; };
    

    the structure struct s has a flexible array member d. [...]

    22 Following the above declaration:

    struct s t1 = { 0 }; // valid
    struct s t2 = { 1, { 4.2 }}; // invalid
    t1.n = 4; // valid
    t1.d[0] = 4.2; // might be undefined behavior
    

    The initialization of t2 is invalid (and violates a constraint) because struct s is treated as if it did not contain member d. [...]

    But, GCC allows the static initialization of flexible array:

    GCC Manual: 6.17 Arrays of Length Zero:

    Instead GCC allows static initialization of flexible array members. This is equivalent to defining a new structure containing the original structure followed by an array of sufficient size to contain the data. E.g. in the following, f1 is constructed as if it were declared like f2.

     struct f1 {
       int x; 
       int y[];
     } f1 = { 1, { 2, 3, 4 } };
    
     struct f2 {
       struct f1 f1; 
       int data[3];
     } f2 = { { 1 }, { 2, 3, 4 } };
    
    0 讨论(0)
提交回复
热议问题