Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

What is the declaration int *((*(*p[5])()))[10]; ?

Pointers

What is the declaration int *((*(*p[5])()))[10]; ?

by  ankan  Posted    (Edited  )
One reason why I wrote this FAQ is that I did not find one dealing with such a controversial (and sometimes confusing) topic as this in the forum. Another (perhaps the main reason!!) is that I had not written any FAQ yet :).

If you ever had difficulties dealing with pointers (you would have certainly had, if you are a C programmer) then go through this FAQ. Otherwise don't bother.

In the following declarations, p is ...
int *p; pointer to int
int *p[10]; array[10] of pointers to int (not same as a 2D array)
int (*p)[10]; pointer to array[10] of int
int *p(); function returning pointer to int
int (*p)(); pointer to function returning int
int *(*p)(); pointer to function returning pointer to int
int (*p[])(); array[] of pointers to function returning int
int (*(*p())[5])(); function returning pointer to array[5] of pointers to function returning int

The above become quite clear when we consider the precedence and associativity of operators:
() [] {left to right}
* {right to left}

As an example what is p in the declaration:
int *((*(*p[5])()))[10];

One nice and easy way to come to the correct conclusion is by assuming that you are the compiler and going through the following obvious steps. Obvious when you keep in mind the precedence and associativity of the operators at hand that is [], ( ), *.

First we have [color blue]p[/color][5] which is obviously an array, so we have
array[5] of

Next is *[color blue]p[5][/color], which is a pointer, thus
array[5] of pointers to

Then [color blue](*p[5])[/color](), which represents a function, hence
array[5] of pointers to function returning

Then *[color blue](*p[5])()[/color], a pointer hence
array[5] of pointers to function returning pointer to

Next consider [color blue]((*(*p[5])()))[/color][10], which is an array, so we get
array[5] of pointers to function returning pointer to array[10] of
(an extra pair of ( ) above is of no use although it does no harm)

Then comes *[color blue]((*(*p[5])()))[10][/color], which is a pointer, so we get
array[5] of pointers to function returning pointer to array[10] of pointers to

Lastly, the type which is int in this case which ultimately leads us to
array[5] of pointers to function returning pointer to array[10] of pointers to int

When you are writing code all you have to do is to follow the reverse of the above steps, which is fairly easy.

Such a complicated (?? if you have gone through the above, it should seem too simple now:)) pointer/array/function/etc is seldom used, I think.

And a nicer and more foolproof way to use such a thing is by using typedef. But that's another story ... ;-)

Bye.
Ankan.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top