By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,961 Members | 1,332 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,961 IT Pros & Developers. It's quick & easy.

a mysterious declaration statement

P: n/a
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

Jun 16 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
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
Jun 16 '07 #2

P: n/a
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
Jun 16 '07 #3

P: n/a
On 6 16 , 10 07 , "Bo Persson" <b...@gmb.dkwrote:
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

Jun 17 '07 #4

P: n/a
Thanks a lot, hope not many of my colleagues will write programs like
this. :)
Jess

Jun 17 '07 #5

P: n/a
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

Jun 17 '07 #6

P: n/a
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
Jun 17 '07 #7

P: n/a
On Jun 17, 1:43 pm, Jess <w...@hotmail.comwrote:
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: ja*********@gmail.com
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

Jun 17 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.