Vidhi Vidhi - 18 days ago 7
C Question

Complex declaration in C- Programming language

I came across a very complex declaration which I could not understand:

char ( * ( * f[3]) ()) [5] ;


I want to know the procedure to determine what is being declared.
Also, since this question was asked to me in an interview, I want to know whether these kind of codes are actually used in the software industry ?

If not, then what is purpose of asking ?

Answer

Simply break the declaration into parts.
The inner most pair of braces : ( * f[3] ) means 'f' is an array of 3 elements which are pointers.

( *f )() means 'a' is a pointer to function, so
( * f[3] ) () means f is an array of 3 pointers to function.

( * ( * f[3] ) ()) means f is an array of 3 pointers to function returning a pointer.

Hence,

char ( * ( * f[3] ) ()) [5]  ;

means f is an array of 3 pointers to function returning a pointer to array of 5 characters

Comments