can someone help with understanding const

i dont know how to explain what im asking but

you have const pointer // pointer can not change
you have const data // data can not change
you have const pointer to const data // neither can change

however there are other ways that the data and the pointer can be changed even though they are constant. can you show me both ways what the const does and how it can be changed anyway.
I smell a hint of a homework problem, but it's disguised pretty well if that's the case ;P

Anyway, say you have a pointer to a const:

 
const int* p = &foo;


You might look at this and think "p points to a variable that is const". That's true, but also not quite true at the same time.

"p points to a const int" is true in that you cannot modify *p.

However it's also false because the data p points to may not actually be const, and might be changed elsewhere in the program.

A typical example:

1
2
3
4
5
6
7
8
int foo = 0;  // a non-const int
const int* p = &foo;  // pointer to const int

cout << *p;  // prints 0 as you'd expect

foo = 1;  // we're changing *p!  but it's totally legal

cout << *p;  // proof that *p has changed, even though it's "const" 
thanks disch. no its not a homework assignment. i was actually reading an article online and it was explaining both the constantness and how it can be broken and it got confusing fast. since the code was all jumbled together
Topic archived. No new replies allowed.