I don't know what this "feature" is called so I coudn't google it also I'm sorry if the title doesn't make sense. I recently looked at suckless dwm's source and saw this code: (from dwm.c)
static int (*xerrorxlib)(Display *, XErrorEvent *);
And also this:
static void (*handler[LASTEvent]) (XEvent *) = {
[ButtonPress] = buttonpress,
[ClientMessage] = clientmessage,
[ConfigureRequest] = configurerequest,
[ConfigureNotify] = configurenotify,
[DestroyNotify] = destroynotify,
[EnterNotify] = enternotify,
[Expose] = expose,
[FocusIn] = focusin,
[KeyPress] = keypress,
[KeyRelease] = keypress,
[MappingNotify] = mappingnotify,
[MapRequest] = maprequest,
[MotionNotify] = motionnotify,
[PropertyNotify] = propertynotify,
[UnmapNotify] = unmapnotify
};
What does void (*handler[LASTEvent]) (XEvent *) mean ? What it is called and why it is used for ?
This declaration
is a declaration of a pointer to function with the name
xerrorxlibthat ( the function) has the return typeintand two parameters of pointer typesDisplay *andXErrorEvent *.The pointer has the static storage duration.
This declaration
declares an array with the name
handlerofLASTEventelements of pointers to function with the return typevoidand the parameter typeXEvent *. The array also has the static storage duration.As for records like this
then in the square brackets there is an index of the array element that is initialized.
For example you can write
and then to use the enumerators in the braced init list in an array declaration like
In this case the array will have two elements where the element with the index 0 is initialized by the value 10 and the element with the index 1 is initialized with the value 20.