简体   繁体   中英

Please explain the use of ':' and a trailing ',' in this struct initialization C code

static struct file_operations memory_fops = {
    open:       memory_open,    /* just a selector for the real open */
};

this is from mem.c file in uclinux

That's GNU-style initialization syntax; the open member is initialized to memory_open , the rest is left uninitialized. C99 uses a different syntax ( .open = memory_open ).

In C the optional trailing comma was allowed in brace-enclosed initializers since the beginning of time. It is there so that you can use uniform comma placement in initializers like

struct SomeStructType s = {
  value1,
  value2,
  value3,
};

This makes it easier, for example, to rearrange the initializers in the list, should such a need arise. Whether you want to use it or not is a matter of personal preference.

As for the : syntax, it is a GCC-specific extension as @geekosaur already explained. The corresponding functionality was standardized in C99 with a different syntax.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM