C – What does this declaration mean?

  • A+
Category:Languages

I am analyzing a multithreading code. In an initialization function, there is a piece of code like this:

for(i=0;i<MAX_STREAMS;i++){     cmdStreamTaskPtr[i] = NULL; } 

I understand that syntax, but my problem is in the declaration of cmdStreamTaskPtr. It is defined in the following way, where cmdData_t is a typedef struct and MAX_STREAMS is five.

static cmdData_t *(*cmdStreamTaskPtr[MAX_STREAMS])(void) = {[0 ... MAX_STREAMS-1] = NULL}; 

I have not idea what does this line mean. Is it a variable with default value?

 


cmdStreamTaskPtr is an array:

cmdStreamTaskPtr[MAX_STREAMS] 

Of pointers:

*cmdStreamTaskPtr[MAX_STREAMS] 

To functions that accept no arguments:

(*cmdStreamTaskPtr[MAX_STREAMS])(void) 

And return a cmdData_t *:

cmdData_t *(*cmdStreamTaskPtr[MAX_STREAMS])(void) 

And is static:

static cmdData_t *(*cmdStreamTaskPtr[MAX_STREAMS])(void) 

That array is then initialized with NULL for all array members:

static cmdData_t *(*cmdStreamTaskPtr[MAX_STREAMS])(void) = {[0 ... MAX_STREAMS-1] = NULL}; 

Note that the initialization syntax [0 ... MAX_STREAMS-1] is not standard C but an extension supported by GCC. It's also redundant in this case because the array is declared as static, meaning it has static storage duration and therefore its elements are implicitly initialized to NULL if not explicitly initialized.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: