Re: decoding a declaration

Discussion in 'C++' started by Ben Bacarisse, Feb 16, 2010.

  1. "t" <> writes:

    > I am trying to decode a declaration "void *(*p[]) (void *a, int n)"
    >
    > Does it mean an array of pointers to a function taking
    > two parameters(one a void pointer, another an integer)
    > and returning a void pointer?


    Yup. That's it.

    > Or am I missing something? Which would be a good book
    > to understand how to decode such declarations?


    I don't know of a book, but you seem to have the hang of it. You read
    inside out from the name (respecting brackets), moving left in
    preference to moving right.

    There's a utility -- cdecl that can turn them into text. I don't know
    if it works for C++ (I just noticed the cross-post!).

    <snip>
    --
    Ben.
     
    Ben Bacarisse, Feb 16, 2010
    #1
    1. Advertising

  2. On Tue, 16 Feb 2010 21:28:25 +0000, Ben Bacarisse
    <> wrote:

    > "t" <> writes:
    >
    > > I am trying to decode a declaration "void *(*p[]) (void *a, int n)"
    > >
    > > Does it mean an array of pointers to a function taking
    > > two parameters(one a void pointer, another an integer)
    > > and returning a void pointer?

    >
    > Yup. That's it.
    >
    > > Or am I missing something? Which would be a good book
    > > to understand how to decode such declarations?

    >
    > I don't know of a book, but you seem to have the hang of it. You read
    > inside out from the name (respecting brackets), moving left in
    > preference to moving right.
    >

    Other way; array and function to the right must be taken before
    pointer (or C++ reference) to the left.
    Overridden by en_GB brackets = en_US parentheses, to be clear.
     
    David Thompson, Mar 4, 2010
    #2
    1. Advertising

  3. Ben Bacarisse

    Stefan Ram Guest

    David Thompson <> writes:
    >Other way; array and function to the right must be taken before
    >pointer (or C++ reference) to the left.


    Yes, and these are just the same precedence rules as for
    expressions. Therefore, earlier in this thread, I suggested
    mentally translating it to an assertion about the type of an
    expression. Someone who already knows the precedence rules
    for expressions, does not have to learn any additional
    precendence rule for declarations.

    When people learn latin or french, they learn literally
    several ten thousands words and declension rules, so it
    might not be asked too much that a C programmer learns
    the priorities of one or two dozens of operators.
     
    Stefan Ram, Mar 4, 2010
    #3
  4. David Thompson <> writes:

    > On Tue, 16 Feb 2010 21:28:25 +0000, Ben Bacarisse
    > <> wrote:
    >
    >> "t" <> writes:
    >>
    >> > I am trying to decode a declaration "void *(*p[]) (void *a, int n)"
    >> >
    >> > Does it mean an array of pointers to a function taking
    >> > two parameters(one a void pointer, another an integer)
    >> > and returning a void pointer?

    >>
    >> Yup. That's it.
    >>
    >> > Or am I missing something? Which would be a good book
    >> > to understand how to decode such declarations?

    >>
    >> I don't know of a book, but you seem to have the hang of it. You read
    >> inside out from the name (respecting brackets), moving left in
    >> preference to moving right.
    >>

    > Other way;


    Would you believe I get left and right confused? Extraordinary but
    true. I usually pause and check multiple times when I write either
    but even then this can happen. I don't confuse the /directions/ its
    is their, to me, arbitrary names that cause the trouble!

    --
    Ben.
     
    Ben Bacarisse, Mar 4, 2010
    #4
  5. Ben Bacarisse

    Stefan Ram Guest

    Ben Bacarisse <> writes:
    >is their, to me, arbitrary names that cause the trouble!


    The names are not arbitrary.

    Look at the word »Left«.

    The »L« of »Left« is a horizontal rule (bar), like »_«.

    But which side of the rule is left?

    There's a vertical mark on the /left/ side of the rule: »L«.

    And, where in the word »Left« is the »L« located?

    On the /left/ side of the word.

    And what happens, when you write »left« and »right« in
    alphabetical order?

    left right

    When you then read this, the word »left« is left and
    the word »right« is right.

    So it all comes out right. (Even in this sentence, the
    word »right« is in the rightmost position.)
     
    Stefan Ram, Mar 4, 2010
    #5
  6. Ben Bacarisse

    Richard Bos Guest

    -berlin.de (Stefan Ram) wrote:

    > Ben Bacarisse <> writes:
    > >is their, to me, arbitrary names that cause the trouble!

    >
    > The names are not arbitrary.
    >
    > Look at the word »Left«.


    > There's a vertical mark on the /left/ side of the rule: »L«.


    > And what happens, when you write »left« and »right« in
    > alphabetical order?
    >
    > left right
    >
    > When you then read this, the word »left« is left and
    > the word »right« is right.
    >
    > So it all comes out right. (Even in this sentence, the
    > word »right« is in the rightmost position.)


    From this, I conclude that Frenchmen and Italians are looking-glass
    creatures.

    Richard
     
    Richard Bos, Mar 10, 2010
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Noah
    Replies:
    5
    Views:
    979
  2. Alex Vinokur
    Replies:
    4
    Views:
    613
    Jonathan Turkanis
    Apr 5, 2004
  3. Ben Bacarisse

    Re: decoding a declaration

    Ben Bacarisse, Feb 16, 2010, in forum: C Programming
    Replies:
    6
    Views:
    284
    Richard Bos
    Mar 10, 2010
  4. Stefan Ram

    Re: decoding a declaration

    Stefan Ram, Feb 16, 2010, in forum: C++
    Replies:
    5
    Views:
    263
    Kleuskes & Moos
    Feb 17, 2010
  5. Stefan Ram

    Re: decoding a declaration

    Stefan Ram, Feb 16, 2010, in forum: C Programming
    Replies:
    5
    Views:
    284
    Kleuskes & Moos
    Feb 17, 2010
Loading...

Share This Page