Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > a mysterious declaration statement

Reply
Thread Tools

a mysterious declaration statement

 
 
Jess
Guest
Posts: n/a
 
      06-16-2007
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

 
Reply With Quote
 
 
 
 
Robert Bauck Hamar
Guest
Posts: n/a
 
      06-16-2007
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
 
Reply With Quote
 
 
 
 
Bo Persson
Guest
Posts: n/a
 
      06-16-2007
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


 
Reply With Quote
 
Chelong
Guest
Posts: n/a
 
      06-17-2007
On 6 16 , 10 07 , "Bo Persson" <(E-Mail Removed)> 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

 
Reply With Quote
 
Jess
Guest
Posts: n/a
 
      06-17-2007
Thanks a lot, hope not many of my colleagues will write programs like
this.
Jess

 
Reply With Quote
 
Jess
Guest
Posts: n/a
 
      06-17-2007
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

 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      06-17-2007
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
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      06-17-2007
On Jun 17, 1:43 pm, Jess <(E-Mail Removed)> 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: http://www.velocityreviews.com/forums/(E-Mail Removed)
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Can a static function declaration conflict with a non-static declaration? nospam_timur@tabi.org C Programming 4 12-12-2006 10:26 PM
maxplusII error: a deferred constant declaration without a full declaration is not supported Noah VHDL 5 04-07-2006 02:34 PM
"virtual outside class declaration" and "declaration does not declare anything" kelvSYC C++ 6 05-17-2005 08:58 AM
Function declaration in class declaration Ovidesvideo C++ 4 12-10-2004 06:36 PM
Intel C++ 8.0 : declaration hides declaration Alex Vinokur C++ 4 04-05-2004 09:49 PM



Advertisments