14

I've come across two pointer declarations that I'm having trouble understanding. My understanding of precedence rules goes something like this:

Operator             Precedence             Associativity
(), [ ]                  1                  Left to Right
*, identifier            2                  Right to Left
Data type                3

But even given this, I can't seem to figure out how to evaluate the following examples correctly:

First example

float * (* (*ptr)(int))(double **,char c)

My evaluation:

  1. *(ptr)
  2. (int)
  3. *(*ptr)(int)
  4. *(*(*ptr)(int))

Then,

  1. double **
  2. char c

Second example

unsigned **( * (*ptr) [5] ) (char const *,int *)
  1. *(ptr)
  2. [5]
  3. *(*ptr)[5]
  4. *(*(*ptr)[5])
  5. **(*(*ptr)[5])

How should I read them?

  • 1
    Asking about how a language handles its features is on-topic here. – user8 Nov 21 '11 at 21:19
7

My guess for the first one: ptr is a pointer to a function that takes as parameter an int, and returns a pointer to a function that takes as parameters a pointer to pointer to double and a char, and returns a pointer to float.

Interpretation:

(*ptr)(int)

says that ptr is a pointer to a function taking an int as an argument. To discover what that function returns we need to expand our view:

(* (*ptr)(int))

this means the function returns a pointer to another function. The parameters of that other function are:

(double **,char c)

and it returns

float *

And for the second one: ptr is a pointer to an array of five pointers to functions that take as parameters a constant pointer to char and a pointer to int, returning a pointer to a pointer of unsigned int.

Interpretation:

( * (*ptr) [5] )

declares ptr as a pointer to array of five pointers to a function taking

(char const *,int *)

as arguments and returning

unsigned **

7

You could try the 'The Clockwise Spiral' Method to understand these insane declarations:

http://c-faq.com/decl/spiral.anderson.html

I blogged about it here too:

http://www.kalekold.net/index.php?post=4

  • Thank you for reply. But in The Clockwise Spiral method from where to start in my case? In the examples given on the link there are simple examples. I am not able to match my equation with that method. – trapaank Nov 21 '11 at 20:48
  • 1
    you should start from your identifier: ptr. you will have: "ptr is a pointer to a function that takes an int and returns a pointer to a function that takes a pointer to a pointer to a double and a char and returns a pointer to a float". try the spyral and you'll get the same. – Remo.D Nov 21 '11 at 21:02
1

It's a function pointer. The person who wrote it could have made better use of typedefs to have made it clearer.

It is in effect a pointer to function with these parameters. float* myfunc(double**, char)

  • 4
    My bet is that the person who wrote it is the teacher. – mouviciel Nov 21 '11 at 21:08
  • Lets hope it isn't the teacher, because you don't need to know crap like this. Though I suppose a good teacher would first slap you in the face with expressions like these, letting you scratch your head trying to interpret them, then show you how to use typedef properly. – user29079 Nov 22 '11 at 7:44
1

Using cdecl.org:

char * const (* (* const bar)[5])(int )

declare bar as const pointer to array 5 of pointer to function (int) returning const pointer to char

unsigned **( * (*ptr) [5] ) ( char const *,int *)

declare ptr as pointer to array 5 of pointer to function (pointer to const char, pointer to int) returning pointer to pointer to unsigned

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.