×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Pointers

What is the declaration int *((*(*p[5])()))[10]; ? by ankan
Posted: 22 Jul 01

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 p[5] which is obviously an array, so we have
array[5] of

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

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

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

Next consider ((*(*p[5])()))[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 *((*(*p[5])()))[10], 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.

Back to C FAQ Index
Back to C Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close