How to interpret this function pointer usage ?

B

Ben Bacarisse

quark020 said:
How to interpret this function pointer usage ?

int*(*q(char*))[];

I've posted this before but not for a while so...

Put your finger on the name (or where the name would be) and read out
the name and the surrounding symbols using these rules:

* Always read to the left first but never past a ')'.
* Then read to the right.
* If you hit the start you are done.
* If you hit a '(' remove everything in the matching '(...)' pair
and start again reading to the left.

As you do this voice the symbols like this:

'*' "pointer to"

'[' "array of" then read what is inside the '[]'

'(' "function taking" and then read using these rules each of the
parameter types (you need to jump to find the where the name
is or would be in each one). For '(void)' just say "no
parameters", and for '()' say "unspecified parameters".
Follow this with "and which returns" and keep going.

'...' "further var-args" (you can re-word this if it too much like
Vogon poetry for you).

You have to add in a few extra words to make real sentences but the
basic idea of going from the inside out and to the left first is a
basic consequence of the formal syntax of the language.

Unfortunately this is not 100% "culture fair" since it relies on a
property of English that "int const" and "const int" probably indicate
the same thing. I have found some people really bothered by this and
they are usually non-native English speakers.

If fails for function parameters of the form 'x[..]'. You can update
the rules to take these into account but it is a bit messy.
 
J

John Bode

How to interpret this function pointer usage ?

int*(*q(char*))[];

TIA

The general procedure for reading hairy declarators is to find the
leftmost identifier and work your way out, remembering that absent any
explicit grouping with parentheses "[]" and "()" bind before "*", so

*a[] -- a is an array of pointer
(*a)[] -- a is a pointer to an array
*f() -- f is a function returning a pointer
(*f)() -- f is a pointer to a function

Sometimes in function parameter lists, you'll see things like "*[]" or
"(*)[]". These are interpreted the same way as above: "*[]"
designates an array of pointer, "(*)[]" designates a pointer to an
array, etc.

Thus, we parse your example as:

q -- q
q( ) -- is a function
q(char*) -- taking one pointer to char
*q(char*) -- returning a pointer
(*q(char*))[] -- to an array
*(*q(char*))[] -- of pointer
int *(*q(char*))[]; -- to int

cdecl's a wonderful tool, but this method's straightforward enough
that you shouldn't need it.
 
Q

quark020

How to interpret this function pointer usage ?
int*(*q(char*))[];

TIA

The general procedure for reading hairy declarators is to find the
leftmost identifier and work your way out, remembering that absent any
explicit grouping with parentheses "[]" and "()" bind before "*", so

     *a[] -- a is an array of pointer
   (*a)[] -- a is a pointer to an array
     *f() -- f is a function returning a pointer
   (*f)() -- f is a pointer to a function

Sometimes in function parameter lists, you'll see things like "*[]" or
"(*)[]".  These are interpreted the same way as above: "*[]"
designates an array of pointer, "(*)[]" designates a pointer to an
array, etc.

Thus, we parse your example as:

         q                  -- q
         q(     )           -- is a function
         q(char*)           --   taking one pointer to char
        *q(char*)           -- returning a pointer
       (*q(char*))[]        -- to an array
      *(*q(char*))[]        -- of pointer
  int *(*q(char*))[];       -- to int

cdecl's a wonderful tool, but this method's straightforward enough
that you shouldn't need it.

Thanks a lot.. :)
 
T

Tim Rentsch

Ben Bacarisse said:
quark020 said:
How to interpret this function pointer usage ?

int*(*q(char*))[];

I've posted this before but not for a while so...

Put your finger on the name (or where the name would be) and read out
the name and the surrounding symbols using these rules:

* Always read to the left first but never past a ')'.
* Then read to the right.
* If you hit the start you are done.
* If you hit a '(' remove everything in the matching '(...)' pair
and start again reading to the left. [...snip...]

Presumably you mean right first, then left, then again to the
right...
 
B

Ben Bacarisse

Tim Rentsch said:
Ben Bacarisse said:
quark020 said:
How to interpret this function pointer usage ?

int*(*q(char*))[];

I've posted this before but not for a while so...

Put your finger on the name (or where the name would be) and read out
the name and the surrounding symbols using these rules:

* Always read to the left first but never past a ')'.
* Then read to the right.
* If you hit the start you are done.
* If you hit a '(' remove everything in the matching '(...)' pair
and start again reading to the left. [...snip...]

Presumably you mean right first, then left, then again to the
right...

Ha! :) Thanks for spotting that.

<aside>When giving directions in a car I have to hold the appropriate
hand out to indicate a direction. I know which is which, of course, but
I can't reliably generate the word associated with it quickly enough.
It take way too long to turn the "thought direction" into a word (and to
verify it) to be of any use when driving. The connection to my hands
seems to be very fast. I am sure this condition has a name -- I can't
think I am the only one.

Not an excuse here, of course, since I had ample time to verify the
words.</aside>
 
M

Mark Bluemel

Ha! :) Thanks for spotting that.

<aside>When giving directions in a car I have to hold the appropriate
hand out to indicate a direction....</aside>

We have trouble when my girlfriend is giving or taking directions in a
car, as she works in operating theatres a lot.

"Take the next left.... NO your left, not the patient's left".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top