-2

Possible Duplicate:
What does dot (.) mean in a struct initializer?
What does [ N ... M ] mean in C aggregate initializers?

struct confd_data_cbs    ssd_shdiag_callback = {
    .callpoint  = show_diag__callpointid_diag_cp,
    .get_object = ssd_common_get_object,
    .get_next   = ssd_common_get_next,
};

.callback, .get_object, .get_next?

Cœur
  • 37,241
  • 25
  • 195
  • 267
Vikram Singh
  • 273
  • 1
  • 3
  • 10

2 Answers2

6

These are called designated initializers (added in C99). They let you specify initializers based on the member names instead of their positions in the structure. Can be kind of handy if you want to initialize some members but not others (and the ones you don't care about initializing aren't all at the end of the struct).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • what is the difference if I initialize using name and if I do it using position. When and which to use ? – Vikram Singh Dec 21 '12 at 06:01
  • 1
    @user1160090: Using names ensures the values go to the intended fields even if the number/order of fields might change. If you only care about C and compilers that support it, I'd use designated initializers most of the time. – Jerry Coffin Dec 21 '12 at 06:04
  • thx Jerry for ur support – Vikram Singh Dec 21 '12 at 06:06
3

This is done both for clarity and for future compatibility.

If you have the structure:

struct confd_data_cbs  {
    TypeA callpoint;
    TypeB get_object;
    TypeC get_next;
};

But at some point later change the definition like so:

struct confd_data_cbs  {
    TypeA callpoint;
    TypeD set_object;   /* New Field Added */
    TypeB get_object;
    TypeC get_next;
};

Then any initializers that don't specify the field names will no longer work.
The ssd_common_get_object will get assigned to set_object, and get_next will be left uninitialized since the order of the fields changed!

When you specify the field names, you know the right fields are getting initialized, even if the order or number of fields changes later.

abelenky
  • 63,815
  • 23
  • 109
  • 159