Reputation: 47824
I saw this code (pulled from device driver source, changed variable names, added comments)
What does the subscript [] do inside the driver_funcs
static array of pointer to void
?
More importantly what's purpose of DECLFUNC
here macro ?
#define DECLFUNC(x) [DRIVER_##x - DRIVER_IOCTL_MIN] f_##x
enum
{
DRIVER_IOCTL_MIN = 300,
DRIVER_GET_PARTITION_STATUS = DRIVER_IOCTL_MIN,
DRIVER_SET_PARTITION_MODE,
DRIVER_GET_PROCESS_ID,
//...
};
static int f_GET_PARTITION_STATUS( int a, int b, int c )
{
//...
return 1;
}
static int f_SET_PARTITION_MODE( int a, int b, int c )
{
//...
return 2;
}
static int f_GET_PROCESS_ID( int a, int b, int c )
{
//...
return 3;
}
static void *driver_funcs[] =
{
DECLFUNC(GET_PARTITION_STATUS),
//=> [0] f_GET_PARTITION_STATUS, => *(f_GET_PARTITION_STATUS+0) ???
DECLFUNC(SET_PARTITION_MODE),
//=> [1] f_SET_PARTITION_MODE, => *(f_SET_PARTITION_MODE+1) ???
DECLFUNC(GET_PROCESS_ID),
//=> [2] f_GET_PROCESS_ID, => *(f_GET_PROCESS_ID+2) ???
//....
};
driver_funcs
is used in normal way
func = driver_funcs[cmd - DRIVER_IOCTL_MIN];
func(1,2,3);
This code is compiled using gcc, if that matters (gcc extension ?). Also if its duplicate, please let me know I'll delete this post.
Upvotes: 2
Views: 145
Reputation: 409374
It's a feature in the C99 standard, to be able to place items at arbitrary positions an in array initializer. See e.g. this GCC manual page.
What it does is to place a function pointer for e.g. GET_PARTITION_STATUS
at index zero in the array.
The C99 standard mandates that there should be a =
between the index and the value, GCC as an extension of the language allows you to omit it.
Upvotes: 4