My question is that as per my sample, I have been able to point to a non-constant variable by a pointer which is meant to point to a constant variable. My sample goes like this :-
int tobepointed = 10;
int tobepointed1 = 11;
int const *ptr = &tobepointed;
cout << "\nPointer points to the memory address: " << ptr;
cout << "\nPointer points to the value: " << *ptr;
tobepointed = 20;
cout << "\nPointer points to the memory address: " << ptr;
cout << "\nPointer points to the value: " << *ptr;
ptr = &tobepointed1;
cout << "\nPointer points to the memory address: " << ptr;
cout << "\nPointer points to the value: " << *ptr;
Now this block of code goes on correctly without any compile or runtime error.
Also consider the pointer *ptr. If i declare the ptr like any normal pointer like :-
int *ptr;
Then also the output is same, so why do we need the concept of 'pointer to constant'?
Yes I agree to the concept of 'constant pointer', but the 'pointer of concept' looks to be of no use.