Hi Brian,
I've had a bit of a scout around on this. I've not had the need to do this before so I've never really investigated it. So I'm no expert. But based on what I've just read and what seems to work in Visual Studio and gcc, this is how I think it works.
If you want to declare a constant pointer to a normal variable, this is how it's done:
int a = 5;
int *const pa = &a;
*pa = 10; // The value of a can be altered through the pointer pa.
You will be able to alter the value of 'a', but you won't be able to alter the value of 'pa' - you'll get a compiler error.
If you want to declare a normal pointer to a constant variable, this is how it's done:
const int a = 5;
const int* pa = &a;
In this case, you'll be able to alter value of 'pa' but you won't be able to change the value of 'a' through the pointer. You'll again get a compiler error
if you try to do that.
You can achieve a similar effect by leaving out the const in the declaration of 'a'.
int a = 5;
const int* pa = &a;
There is a subtle difference here though. You won't be able to change the value of 'a' through the pointer, but you will be able to change it using a normal assignment statement.
If you want to declare a constant pointer to a constant variable, this is how it's done:
const int a = 5;
const int *const pa = &a;
In this case you won't be able to alter either the value of 'a' or the address pointed to by 'pa'.
I think that covers most of the scenarios you might want. I've been trying to think of a succint way you can reason it out when you're writing code. What follows is my best effort.
*** If you want to make a pointer constant, you can add '*const' after the type specifier (the int or char or float or whatever) in the pointer declaration.
*** If the variable that you are pointing to is a constant, or you wish to make it a constant as far as the pointer is concerned, you can place the keyword 'const' before the type specifier in the pointer declaration.
I hope that helps. Sometimes for issues like this where C++ is very similar to C, it's worth looking at some C documentation. It can sometimes point in the right direction. If you want more info take a look at:
http://msdn.microsoft.com/library/de...clarations.asp
All the best with your programming.
Dan