69

looking at the linux kernel source, I found this:

static struct tty_operations serial_ops = {
  .open = tiny_open,
  .close = tiny_close,
  .write = tiny_write,
  .write_room = tiny_write_room,
  .set_termios = tiny_set_termios,
};

I've never seen such a notation in C. Why is there a dot before the variable name?

user13267
  • 6,871
  • 28
  • 80
  • 138
c0de
  • 1,132
  • 1
  • 10
  • 10
  • 1
    This is used for *initialization* of struct members (they are not variables) in the "new" C99 style (some compilers may have used the same format even before C99 was issued). – pmg Sep 20 '11 at 15:45

2 Answers2

56

This is a Designated Initializer, which is syntax added for C99. Relevant excerpt:

In a structure initializer, specify the name of a field to initialize with ‘.fieldname =’ before the element value. For example, given the following structure,

struct point { int x, y; }; 

the following initialization

struct point p = { .y = yvalue, .x = xvalue }; 

is equivalent to

struct point p = { xvalue, yvalue };
Brian Peterson
  • 2,800
  • 6
  • 29
  • 36
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
14

It's sometimes called "designated initialization". This is a C99 addition, though it's been a GNU extension for a while.

In the list, each . names a member of the struct to initialize, the so called designator.

sidyll
  • 57,726
  • 14
  • 108
  • 151