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

const against non const pointer

P: n/a
Hello,

if i have these two declarations:

1)const int * test;

and

2)int* const test;
Does the first mean that the value of the integer cannot be changed and
the second one that the location to which the pointer points to cannot
be changed...? or is the other way around?

matti

Nov 7 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a

mat...@gmx.at wrote:
Hello,

if i have these two declarations:

1)const int * test;

and

2)int* const test;
Does the first mean that the value of the integer cannot be changed and
the second one that the location to which the pointer points to cannot
be changed...? or is the other way around?
You have it right.

Nov 7 '06 #2

P: n/a
ma****@gmx.at wrote:
if i have these two declarations:

1)const int * test;

and

2)int* const test;
Does the first mean that the value of the integer cannot be changed and
the second one that the location to which the pointer points to cannot
be changed...? or is the other way around?
See the FAQ:

http://www.parashift.com/c++-faq-lit....html#faq-18.5

Cheers! --M

Nov 7 '06 #3

P: n/a
ma****@gmx.at wrote:
if i have these two declarations:

1)const int * test;

and

2)int* const test;
Does the first mean that the value of the integer cannot be changed and
the second one that the location to which the pointer points to cannot
be changed...? or is the other way around?
The first variant means that the integer the pointer points to cannot be changed
through pointer 'test' without an explicit cast. Note that it is still possible
that the integer can be changed through some other access path (directly or
through some other pointer). Also note that if the integer itself is not a
constant object, it is perfectly legal to cast away the constness an change it
through 'test':

*const_cast<int*>(test) = 42;

The second variant means that pointer 'test' cannot be changed (as I understand,
that what you meant by "the location to which the pointer points to cannot be
changed").

--
Best regards,
Andrey Tarasevich
Nov 7 '06 #4

P: n/a
ma****@gmx.at wrote:
Hello,

if i have these two declarations:

1)const int * test;

and

2)int* const test;
Does the first mean that the value of the integer cannot be changed and
the second one that the location to which the pointer points to cannot
be changed...? or is the other way around?

matti
Others have already confirmed that you are correct. As a tip, these
sorts of declarations, notably ones with brackets or parentheses, can in
general be read right to left:

1) test is a pointer to an int constant
2) test is a constant pointer to an int

For more complicated declarations, check out:

http://www.ericgiguere.com/articles/...larations.html
(it's a bit old and written for C, but the basic ideas still hold)

Finally, there's a handy little UNIX utility that will convert
declarations to english and vice versa. Look for cdecl or c++decl.
Nov 7 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.