a mysterious declaration statement

J

Jess

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
 
R

Robert Bauck Hamar

Jess said:
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.
 
B

Bo Persson

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
 
C

Chelong

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
 
J

Jess

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
 
J

John Harrison

Jess said:
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
 
J

James Kanze

I just remembered another question. I think the declaration
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,825
Latest member
VernonQuy6

Latest Threads

Top