I was examining some code on github https://github.com/umlaeute/v4l2loopback/blob/master/v4l2loopback.c and came across this line, which baffles me. Is this some incredibly cool kernel macro or gcc feature that I don’t know about? What does the = -1 do?
static int video_nr[MAX_DEVICES] = { [0 ... (MAX_DEVICES-1)] = -1 };
module_param_array(video_nr, int, NULL, 0444);
MODULE_PARM_DESC(video_nr, "video device numbers (-1=auto, 0=/dev/video0, etc.)");
The line in question is the first, the next two given for context (this is creating a cmdline-specifiable parameter using a kernel macro http://lxr.free-electrons.com/source/include/linux/moduleparam.h#L103 )
Anyway, what is going on with the array initialization? How does that syntax work?
You’ve found an example of designated initializers. C99 & C11 don’t go quite as far as your example, but they have some pretty flexible support for this kind of behaviour. Your specific example (using the
...) is a GCC extension. From the link:So that means your example is creating an array of size
MAX_DEVICESand initializing every element in that array to-1.For reference, the only standard-supported behaviour is to assign specific indices, rather than ranges:
There is a more complicated example in my copy of the spec:
Which initializes the first five and last five elements of the array to explicit values. The middle values (if any) would be
0.