cpointersstructcompound-literals

C: static initialisation of compound struct with pointers


I have a nested C (gcc-11) struct which contains an array of pointers to another struct. I would like to create a static variable of this type and initialise it at the same time. And all these within a header file. The reason is that it represents some default values which belong to that header file. I don't want to call an initialisation function to set it.

the following compiles OK:

typedef struct s1_s {
        int a;
        int b;
} s1_t;

typedef struct s2_s {
        int c;
        s1_t *s1s; // but I need this to be **s1s really!
} s2_t;

s2_t *def = &(s2_t){
        .c = 10,
        .s1s = &(s1_t){  
                .a = 100,
                .b = 200 
        } 
};
int main(void){ return 1; }

But I would like an array of pointers: s1_t **s1s, to be precise. This does not compile:

typedef struct s1_s {
        int a;
        int b;
} s1_t;

typedef struct s2_s {
        int c;
        s1_t **s1s; // "array" of pointers
} s2_t;

s2_t *def = &(s2_t){
        .c = 10,
        // I want an "array" of 1 *(s1_t*) element
        .s1s = &(s1_t *){{  
                .a = 100,
                .b = 200 
        }} 
};
int main(void){ return 1; }

These are the first errors:

warning: braces around scalar initializer
   15 |         .s1s = &(s1_t *){{
error: field name not in record or union initializer
   16 |                 .a = 100,
...

Solution

  • Just do this:

    s2_t *def = &(s2_t){
            .c = 10,
            // I want an "array" of 1 *(s1_t*) element
            .s1s = (s1_t*[]){&(s1_t){  
                    .a = 100,
                    .b = 200 
            }} 
    };