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

this is from mem.c file in uclinux

mike
  • 31
  • 3

2 Answers2

6

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).

geekosaur
  • 59,309
  • 11
  • 123
  • 114
  • 1
    Right. It's worth noting that this is probably really old code: the GCC docs say the old GNU style syntax is "obsolete since GCC 2.5", which was released in 1993. :-) – Ken Mar 17 '11 at 01:30
  • 2
    Are you sure about "left uninitialized" part? Normally in C it is all-or-nothing, i.e. if at least *something* is initialized, then everything else is *zero-initialized*. That's how it works in C99 with the new syntax. Of course, a GCC-specific extension can behave differently. – AnT stands with Russia Mar 17 '11 at 01:47
  • I don't know for certain, but I would expect old-style behavior with the old-style initializer: globals are initialized to 0, `auto` (stack) variables will be whatever trash was using that memory space before. – geekosaur Mar 17 '11 at 01:48
  • Well, once again, with aggregate initializers C has always followed that policy very strictly: if some struct (or array) object is partially initialized by the user, then the rest is zero-initialized. The only way to see "trash" in a variable is not to initialize it at all. Not even a part of it. – AnT stands with Russia Mar 17 '11 at 01:51
  • ... and that *is* old-style behavior, BTW. – AnT stands with Russia Mar 17 '11 at 03:00
6

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.

AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • So the beginning of time for you is C99 ? interesting :) I think trailing commas (for initializers and `enum`) have been added then. – Jens Gustedt Mar 17 '11 at 08:54
  • @Jens Gustedt: No. In C99 trailing commas became allowed in *enums* specifically. And one of the reasons they were added was to remove the ages-old inconsistency between enums and brace-enclosed initializers. In the latter the trailing comma was allowed since the beginning of time, as I said above (at least since C89/90), while in enums the trailing comma was illegal before C99. – AnT stands with Russia Mar 18 '11 at 05:37
  • right. I am convinced that I had read that somewhere, but searching around on the web didn't bring up anything, my bad. – Jens Gustedt Mar 18 '11 at 07:46
  • It also makes code generation routines easier (like some sort of utility tool that parses some data file and turns it into C source) as it removes the need to check if a comma needs inserting or not. – Piku Feb 01 '20 at 20:23