What does a dot before the variable name in struct mean?

CLinuxKernel

C Problem Overview


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?

C Solutions


Solution 1 - C

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 };

Solution 2 - C

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionc0deView Question on Stackoverflow
Solution 1 - CReed CopseyView Answer on Stackoverflow
Solution 2 - CsidyllView Answer on Stackoverflow