Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Complex declarations

Tags:

c

declaration

How do I interpret complex declarations like:

int * (* (*fp1) (int) ) [10]; ---> declaration 1
int *( *( *[5])())(); --------> declaration 2

Is there any rule that should be followed to understand the above declarations?

like image 588
Prasoon Saurav Avatar asked Dec 12 '09 10:12

Prasoon Saurav


1 Answers

Here is a great article about how to read complex declarations in C: http://www.codeproject.com/KB/cpp/complex_declarations.aspx

It helped me a lot!

Especially - You should read "The right rule" section. Here quote:

int * (* (*fp1) (int) ) [10]; This can be interpreted as follows:

  1. Start from the variable name -------------------------- fp1
  2. Nothing to right but ) so go left to find * -------------- is a pointer
  3. Jump out of parentheses and encounter (int) --------- to a function that takes an int as argument
  4. Go left, find * ---------------------------------------- and returns a pointer
  5. Jump put of parentheses, go right and hit [10] -------- to an array of 10
  6. Go left find * ----------------------------------------- pointers to
  7. Go left again, find int -------------------------------- ints.
like image 188
matekm Avatar answered Oct 11 '22 19:10

matekm