question about const keyword in pointers

hi, started to read book Lipman C++ and have first questions about pointers,
here is my code
1
2
3
4
5
int i = 2;
int j = 4;
	
int const * pi = &i; //const pointer to int type
pi = &j; 

i compile this code in VS2008, and it's success, but why, i change the value of pi?
Last edited on
1
2
3
const int* pi;  // A
int const* pi;  // A
int* const pi;  // B 


above 'A' code makes a nonconst pointer which points to a const variable. IE: pi is not const... but what it points to is const.

'B' is the opposite. the pointer is const, but what it points to is not.
thanks alot, is
const int * and int const * same at all, or have some difference?
They are the exact same.
what about const int const * const const pi; why compiler doesn't panish?
That type is invalid. Firstly, 'int' is being made const twice. Secondly, there's two const in a row after the asterisk.
'int const * const' or 'const int * const' would be valid, though.
helios, i think so, but when i try it, it works, i use VS2008, also i tried to write like this
1
2
int i = 1;
const int const * const pi = &i;

and it's works, i don't understand why?
Probably an overly permissive compiler.
test.cpp: In function `int main()':
test.cpp:306: error: duplicate `const'
test.cpp:306: error: duplicate `const'


EDIT: I just tried it with VC++. It doesn't produce an error, but it does produce a warning:
warning C4114: same type qualifier used more than once
warning C4114: same type qualifier used more than once
Last edited on
Topic archived. No new replies allowed.