Connecting Tech Pros Worldwide Forums | Help | Site Map

a mysterious declaration statement

Jess
Guest
 
Posts: n/a
#1: Jun 16 '07
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


Robert Bauck Hamar
Guest
 
Posts: n/a
#2: Jun 16 '07

re: a mysterious declaration statement


Jess wrote:
Quote:
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
Bo Persson
Guest
 
Posts: n/a
#3: Jun 16 '07

re: a mysterious declaration statement


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


Chelong
Guest
 
Posts: n/a
#4: Jun 17 '07

re: a mysterious declaration statement


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

Jess
Guest
 
Posts: n/a
#5: Jun 17 '07

re: a mysterious declaration statement


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

Jess
Guest
 
Posts: n/a
#6: Jun 17 '07

re: a mysterious declaration statement


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

John Harrison
Guest
 
Posts: n/a
#7: Jun 17 '07

re: a mysterious declaration statement


Jess wrote:
Quote:
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
Quote:
"*p()" always has "p" binding more tightly to the right?
* has a higher precedence than (), so yes.
Quote:
>
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
James Kanze
Guest
 
Posts: n/a
#8: Jun 17 '07

re: a mysterious declaration statement


On Jun 17, 1:43 pm, Jess <w...@hotmail.comwrote:
Quote:
I just remembered another question. I think the declaration
Quote:
int *p()
Quote:
says p is a function that takes no argument and returns a pointer to
an int. What about the following?
Quote:
int (*p)()
Quote:
I think it says p is a pointer that points to a function taking no
argument and returns an int. Is this right?
Right.
Quote:
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: james.kanze@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

Closed Thread