Can anyone explain to me how this initialization structure actually works?

struct audio_policy_service_ops {

audio_io_handle_t (*open_duplicate_output)(void *service,audio_io_handle_t output1,  
     int (*close_output)(void *service, audio_io_handle_t output);audio_io_handle_t output2);
     int (*suspend_output)(void *service, audio_io_handle_t output);
     int (*restore_output)(void *service, audio_io_handle_t output);
};

      

They are now initialized as shown below:

struct audio_policy_service_ops aps_ops = {
    open_duplicate_output : aps_open_dup_output,
    close_output          : aps_close_output,
    suspend_output        : aps_suspend_output,
    restore_output        : aps_restore_output,
};

      

+3


source to share


2 answers


This is GCC's non-standard initialization syntax for structs. It is colloquially referred to as the old-style GNU style init syntax. Its standard equivalent is something like

struct Foo bar = { .name1 = value1, .name2 = value2 };

      



You can read about this in the GCC documentation .

+7


source


This is GCC-ism. When initializing a structure, one could prefix the initialization values ​​with the name of the structure field. This allows the initializers to follow random order and gives a clearer idea of ​​which field the given values ​​are in.



+1


source







All Articles