a mysterious declaration statement

Discussion in 'C++' started by Jess, Jun 16, 2007.

  1. Jess

    Jess Guest

    Hello,

    In the appendix of Accelerated C++, there is a declaration statement

    const char * const * const * cp;

    The author says the specifiers are "const char" and the declarator is
    "* const * const * cp". What does this declaration mean?

    Thanks,
    Jess
     
    Jess, Jun 16, 2007
    #1
    1. Advertising

  2. Jess wrote:

    > Hello,
    >
    > In the appendix of Accelerated C++, there is a declaration statement
    >
    > const char * const * const * cp;
    >
    > The author says the specifiers are "const char" and the declarator is
    > "* const * const * cp". What does this declaration mean?


    First of all: C++ splits declarations up in two. The type name on the left,
    and something that looks like an expression using the variable declared on
    the rigth. So in

    char *p;

    the type is char, and the expression (or declarator) is *p, saying that an
    expression of the form *p is a char, and thus, p must be a pointer to char.

    This form of declaration was passed on from C, that at the time had neither
    references nor const. Many C++ programmers today will write

    char* p;

    Imagining it says: The name is p, and its type is char*, and many will also
    read char* backwards, so it's read pointer to char.

    const char * const * const * cp;

    which says:
    ***cp is a const char
    **cp is a const pointer
    *cp is a const pointer
    cp is a non-const pointer.

    and is the same as

    char const * const * const * cp;

    and read from behind: cp is a pointer to a const pointer to a const pointer
    to a const char.

    --
    rbh
     
    Robert Bauck Hamar, Jun 16, 2007
    #2
    1. Advertising

  3. Jess

    Bo Persson Guest

    Jess wrote:
    :: Hello,
    ::
    :: In the appendix of Accelerated C++, there is a declaration
    :: statement
    ::
    :: const char * const * const * cp;
    ::
    :: The author says the specifiers are "const char" and the declarator
    :: is "* const * const * cp". What does this declaration mean?
    ::
    :: Thanks,
    :: Jess

    Declations like this is usually best to read from right to left. If
    you do, you get that cp is a pointer (*) to a const pointer (* const)
    to another const pointer (* const) that points to a const char.

    Don't do that in your program! :)


    Bo Persson
     
    Bo Persson, Jun 16, 2007
    #3
  4. Jess

    Chelong Guest

    On 6 16 , 10 07 , "Bo Persson" <> wrote:
    > Jess wrote:
    >
    > :: Hello,
    > ::
    > :: In the appendix of Accelerated C++, there is a declaration
    > :: statement
    > ::
    > :: const char * const * const * cp;
    > ::
    > :: The author says the specifiers are "const char" and the declarator
    > :: is "* const * const * cp". What does this declaration mean?
    > ::
    > :: Thanks,
    > :: Jess
    >
    > Declations like this is usually best to read from right to left. If
    > you do, you get that cp is a pointer (*) to a const pointer (* const)
    > to another const pointer (* const) that points to a const char.
    >
    > Don't do that in your program! :)
    >
    > Bo Persson


    u r right
     
    Chelong, Jun 17, 2007
    #4
  5. Jess

    Jess Guest

    Thanks a lot, hope not many of my colleagues will write programs like
    this. :)
    Jess
     
    Jess, Jun 17, 2007
    #5
  6. Jess

    Jess Guest

    I just remembered another question. I think the declaration

    int *p()

    says p is a function that takes no argument and returns a pointer to
    an int. What about the following?

    int (*p)()

    I think it says p is a pointer that points to a function taking no
    argument and returns an int. Is this right? If so, does it mean
    "*p()" always has "p" binding more tightly to the right?

    Thanks,
    Jess
     
    Jess, Jun 17, 2007
    #6
  7. Jess wrote:
    > I just remembered another question. I think the declaration
    >
    > int *p()
    >
    > says p is a function that takes no argument and returns a pointer to
    > an int. What about the following?
    >
    > int (*p)()
    >
    > I think it says p is a pointer that points to a function taking no
    > argument and returns an int. Is this right?


    That's right.

    If so, does it mean
    > "*p()" always has "p" binding more tightly to the right?


    * has a higher precedence than (), so yes.

    >
    > Thanks,
    > Jess
    >


    If you really want to confuse yourself try working out the syntax for a
    function which returns a pointer to a function (without using typedefs).

    john
     
    John Harrison, Jun 17, 2007
    #7
  8. Jess

    James Kanze Guest

    On Jun 17, 1:43 pm, Jess <> wrote:
    > I just remembered another question. I think the declaration


    > int *p()


    > says p is a function that takes no argument and returns a pointer to
    > an int. What about the following?


    > int (*p)()


    > I think it says p is a pointer that points to a function taking no
    > argument and returns an int. Is this right?


    Right.

    > If so, does it mean
    > "*p()" always has "p" binding more tightly to the right?


    The "operators" in the C++ declarations bind exactly as do the
    operators in an expression. Generally speaking, operators which
    appear to the right of the operand have higher precedance than
    those binding to the left, and with those closer to the operand
    being bound first. So something like "int* a[]" is an array of
    pointer, and not a pointer to an array, and "int *p()" is a
    function returning a pointer, and not a pointer to a function.
    And something like "int (*p[])()" is an array of pointers to a
    function returning int.

    --
    James Kanze (Gabi Software) email:
    Conseils en informatique orientée objet/
    Beratung in objektorientierter Datenverarbeitung
    9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
     
    James Kanze, Jun 17, 2007
    #8
    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:
    960
  2. Alex Vinokur
    Replies:
    4
    Views:
    593
    Jonathan Turkanis
    Apr 5, 2004
  3. Ovidesvideo
    Replies:
    4
    Views:
    517
    Andrey Tarasevich
    Dec 10, 2004
  4. kelvSYC
    Replies:
    6
    Views:
    7,234
    Richard Herring
    May 17, 2005
  5. Replies:
    4
    Views:
    1,081
    Richard Tobin
    Dec 12, 2006
Loading...

Share This Page