Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I understand complicated function declarations?

How do I understand following complicated declarations?

char (*(*f())[])();  char (*(*X[3])())[5];  void (*f)(int,void (*)());   char far *far *ptr;  typedef void (*pfun)(int,float);  int **(*f)(int**,int**(*)(int **,int **)); 
like image 922
Pale Blue Dot Avatar asked Sep 19 '09 16:09

Pale Blue Dot


People also ask

What is complicated declaration and evaluation in C?

Complicated declarations in C. Redeclaration of global variable in C. Internal Linkage and External Linkage in C. Different ways to declare variable as constant in C and C++ Variable length arguments for Macros.

What is the problem in the following C declarations?

A function with same name cannot have different return types. A function with same name cannot have different number of parameters. A function with same name cannot have different signatures.


1 Answers

As others have pointed out, cdecl is the right tool for the job.

If you want to understand that kind of declaration without help from cdecl, try reading from the inside out and right to left

Taking one random example from your list char (*(*X[3])())[5];
Start at X, which is the identifier being declared/defined (and the innermost identifier):

char (*(*X[3])())[5];          ^ 

X is

X[3]  ^^^ 

X is an array of 3

(*X[3])  ^                /* the parenthesis group the sub-expression */ 

X is an array of 3 pointers to

(*X[3])()        ^^ 

X is an array of 3 pointers to function accepting an unspecified (but fixed) number of arguments

(*(*X[3])())  ^                   /* more grouping parenthesis */ 

X is an array of 3 pointers to function accepting an unspecified (but fixed) number of arguments and returning a pointer

(*(*X[3])())[5]             ^^^ 

X is an array of 3 pointers to function accepting an unspecified (but fixed) number of arguments and returning a pointer to an array of 5

char (*(*X[3])())[5]; ^^^^                ^ 

X is an array of 3 pointers to function accepting an unspecified (but fixed) number of arguments and returning a pointer to an array of 5 char.

like image 87
pmg Avatar answered Sep 19 '22 09:09

pmg