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.
精彩评论