Stroustrup 5.4.1, pointers and constants

A

arnuld

int a = 1;
const int c = 2;

now Stroustrup declares pointers for "a" and "c":

const int* p1 = &c; // ok
const int* p2 = &a; // he says it is OK

i don't get it, "a" is not a constant and we are declaring a "pointer
to a constant" . what is this ?

after that, is this legal:

int* p3 = &a;

?
 
A

Alf P. Steinbach

* arnuld:
int a = 1;
const int c = 2;

now Stroustrup declares pointers for "a" and "c":

const int* p1 = &c; // ok
const int* p2 = &a; // he says it is OK

i don't get it, "a" is not a constant and we are declaring a "pointer
to a constant" . what is this ?

"pointer to a constant" is a restriction on how that pointer can be
used. p2 cannot be used to change a. It does not guarantee that a
isn't changed by other means, it only guarantees that given p2, and
using only p2, you can't change a.

after that, is this legal:

int* p3 = &a;

?

Yes.
 
A

arnuld

* arnuld:

"pointer to a constant" is a restriction on how that pointer can be
used. p2 cannot be used to change a. It does not guarantee that a
isn't changed by other means, it only guarantees that given p2, and
using only p2, you can't change a.

then what about this:

const int* p2 = &a;

you said i can not change "a" using p2 but i can using some other
pointer. then

int *const p22 = &a;

this means, "a" will *never* be changed. ... right ?


*p3 = 78;

and "a" will get changed to value 78 but i can not use "p3" on:

int *const p22 = &a;

then what does this mean:

const int *const p4 = &a;

?
 
A

Alf P. Steinbach

* arnuld:
On Mar 28, 12:18 pm, "Alf P. Steinbach" <[email protected]> wrote:
* arnuld:

"pointer to a constant" is a restriction on how that pointer can be
used. p2 cannot be used to change a. It does not guarantee that a
isn't changed by other means, it only guarantees that given p2, and
using only p2, you can't change a.

then what about this:

const int* p2 = &a;

you said i can not change "a" using p2 but i can using some other
pointer. then

int *const p22 = &a;

this means, "a" will *never* be changed. ... right ?

No, it means p22 will never be changed.

Reading the declaration backwards, p22 is a constant pointer to int.

Since p22 is constant it cannot be changed; since it's a pointer to
non-constant it can be used to change whatever it points to.
 
A

arnuld

* arnuld:


No, it means p22 will never be changed.

Reading the declaration backwards, p22 is a constant pointer to int.

Since p22 is constant it cannot be changed; since it's a pointer to
non-constant it can be used to change whatever it points to.

ok i will try one more time:

int a = 1;

const int* p = &a;

it is a pointer to a constant, whereas neither "p" nor "a" is not a
constant. it only means can not change "a" using "p" but "some other
pointer" can and we can also make "p" to point some where else.


int *const p1 = &a;

"constant pointer" to a variable. we can not change the "p1" to point
to anything else. we can change the value of "a" using "p1" or some
other pointer.

const int *const p2 = &a;

we can not change anything here.
 
A

Alf P. Steinbach

* arnuld:
ok i will try one more time:

int a = 1;

const int* p = &a;

it is a pointer to a constant, whereas neither "p" nor "a" is not a
constant. it only means can not change "a" using "p" but "some other
pointer" can and we can also make "p" to point some where else.


int *const p1 = &a;

"constant pointer" to a variable. we can not change the "p1" to point
to anything else. we can change the value of "a" using "p1" or some
other pointer.

const int *const p2 = &a;

we can not change anything here.

Yep.
 
A

Alf P. Steinbach

* Juha Nieminen:
Technically speaking, that's not actually true: const_cast<>

Technically speaking there are at least 101 more ways to circumvent the
type safety.

But don't list them, please.
 

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,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top