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

前端 未结 2 1243
忘了有多久
忘了有多久 2020-12-07 10:59

looking at the linux kernel source, I found this:

static struct tty_operations serial_ops = {
  .open = tiny_open,
  .close = tiny_close,
  .write = tiny_wri         


        
相关标签:
2条回答
  • 2020-12-07 11:24

    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 };
    
    0 讨论(0)
  • 2020-12-07 11:30

    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.

    0 讨论(0)
提交回复
热议问题