[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c

How to interpret this function pointer usage ?

quark020

2/9/2011 12:16:00 PM

How to interpret this function pointer usage ?

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

TIA
9 Answers

Mark Bluemel

2/9/2011 12:57:00 PM

0

On 02/09/2011 12:15 PM, quark020 wrote:
> How to interpret this function pointer usage ?
>
> int*(*q(char*))[];

http://www.... tells me

declare q as function (pointer to char) returning pointer to array of
pointer to int

jt

2/9/2011 12:59:00 PM

0

quark020 <quark020@gmail.com> wrote:
> How to interpret this function pointer usage ?

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

http://www.cdecl....*%28*q%28char*%29%29[]

Regards, Jens
--
\ Jens Thoms Toerring ___ jt@toerring.de
\__________________________ http://t...

Ben Bacarisse

2/9/2011 3:07:00 PM

0

quark020 <quark020@gmail.com> writes:

> 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.

--
Ben.

John Bode

2/9/2011 3:11:00 PM

0

On Feb 9, 6:15 am, quark020 <quark...@gmail.com> wrote:
> 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.

Ralf Damaschke

2/9/2011 7:10:00 PM

0

Ben Bacarisse <ben.usenet@bsb.me.uk> wrote:

> 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.

Hmm, is that a trick to claim that the british no longer drive
on the left but on the right?

-- Ralf

quark020

2/10/2011 5:15:00 AM

0

On Feb 9, 8:11 pm, John Bode <jfbode1...@gmail.com> wrote:
> On Feb 9, 6:15 am, quark020 <quark...@gmail.com> wrote:
>
> > 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.. :-)

Tim Rentsch

2/21/2011 3:46:00 AM

0

Ben Bacarisse <ben.usenet@bsb.me.uk> writes:

> quark020 <quark020@gmail.com> writes:
>
>> 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...

Ben Bacarisse

2/21/2011 3:20:00 PM

0

Tim Rentsch <txr@alumni.caltech.edu> writes:

> Ben Bacarisse <ben.usenet@bsb.me.uk> writes:
>
>> quark020 <quark020@gmail.com> writes:
>>
>>> 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>

--
Ben.

Mark Bluemel

2/23/2011 9:27:00 AM

0

On 02/21/2011 03:19 PM, Ben Bacarisse wrote:
> Tim Rentsch<txr@alumni.caltech.edu> writes:

>> 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....</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".