I was just reading some code in the st terminal emulator and came across this syntax:
static void (*handler[LASTEvent])(XEvent *) = {
[KeyPress] = kpress,
[ClientMessage] = cmessage,
/* Removed some lines for brevity ... */
};
I have never seen this syntax in C and I am not even sure what to google for. I have a rough idea what it does (defining handler as an array of function pointers), but I would like to understand this syntax better. It seems to be valid at least in C99, but I am looking for some more details why this is correct, how exactly it works and maybe a pointer to the C standard where this syntax is defined.
The terms foobar (/ˈfuːbɑːr/), foo, bar, baz, and others are used as metasyntactic variables and placeholder names in computer programming or computer-related documentation.
Foo (pronounced FOO) is a term used by programmers as a placeholder for a value that can change, depending on conditions or on information passed to the program. Foo and other words like it are formally known as metasyntactic variables.
Foo is an intentionally meaningless placeholder word often used in computer programming.
Foo is a metasyntactic variable that is commonly used as a placeholder to name variables or code elements. It can be used as the name of a variable, particularly when a programmer wishes to assign a name but doesn't know what name to assign.
This is initializing an array of function pointers with enum indexes. See here.
As mentioned in the comments below uses Designated Initializers.
This short example should show how it can be used.
enum indexes {ZERO, ONE, TWO, FOUR=4};
int array[5] = {[FOUR]=1, [TWO]=9};
for(int i = 0; i < 5; i++)
printf("%d, ", array[i]);
This prints out
0, 0, 9, 0, 1,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With