Constness with pointers to pointers etc.

R

Richard Hayden

Hi,

I understand such pointers as 'const int* const ip' and 'const int*
ip' etc., but I'm getting confused when seeing things like 'const int*
const* ip' (i.e. with two or more asterisks). Clearly pointers to
pointers like this will require an expansion of the syntax to
accommodate for all of the possible extra combinations (i.e. must
specify the constness of the pointer ip and the constness of the
pointer, to which ip is pointing). Is there a reference (or someone
here) which/who can tell me how the syntax for such complicated
pointer declarations works?

Thanks,

Richard Hayden.
 
R

Rob Williscroft

Richard Hayden wrote in @posting.google.com:
Hi,

I understand such pointers as 'const int* const ip' and 'const int*
ip' etc., but I'm getting confused when seeing things like 'const int*
const* ip' (i.e. with two or more asterisks). Clearly pointers to
pointers like this will require an expansion of the syntax to
accommodate for all of the possible extra combinations (i.e. must
specify the constness of the pointer ip and the constness of the
pointer, to which ip is pointing). Is there a reference (or someone
here) which/who can tell me how the syntax for such complicated
pointer declarations works?

const int a;

is another way of writing

int const a;

Its the one and only exception to the rule that a qualifier (const or
volatile) goes after that which it qualifies.

int const * const * const * const cp_to_cp_to_cp_to_int;
int const * * const * const cp_to_cp_to_p_to_int;

From your statement above, the declaration that "confuses" you:

const int* const* ip;

rewrite it "correctly" (;-)/YMMV)

int const * const *ip;

then read it backwards "ip is a pointer to a const pointer to a
const int".

HTH

Rob.
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top