This means p can change, but you can't use it to change the character
pointed at. For example:
p = r; // works if r is a suitable pointer
x = *p; // works if x is a suitable variable
*p = x; // won't work
This means you can't change p, but you can use it to change the
character pointed at. For example:
p = r; // doesn't work
x = *p; // works if x is a suitable variable
*p = x; // also works if x is a suitable variable
I was going to point you to the C FAQ at
http://c-faq.com/ but it
doesn't seem to answer this question. It's still worth a read though.
It's not quite correct to say that you can't do those things, and the
section of the FAQ that Ben referred doesn't go into the relevant issues
either. It would be more accurate to say that any attempt to do those
things is a constraint violation, which requires a diagnostic message.
Making that message mandatory is the main purpose for using 'const' in a
declaration. If you know that a given pointer should not be used to try
to change something, you can insert 'const' in the appropriate part of
the declaration to get the compiler to warn you if your code actually
does attempt to misuse it in that fashion. Either the attempt to change
the corresponding object was a mistake, or the belief that the variable
should not be used to make such changes was a mistake - you'll have to
investigate to determine which one was wrong. But at least, with the
message, you'll know there was a conflict.
However, after having issued a diagnostic, a compiler is still free to
translate your code into a program. If you choose to execute that
program, it's behavior is undefined. That means, among other
possibilities, that the program might in fact change something it should
not have changed (even objects defined as 'const').
You can disable the diagnostic message by casting the pointer to an
appropriate non-const type. Never do this unless you're sure it's both
necessary and correct - consider whether it would be more appropriate to
remove the 'const' from the declaration, rather than casting it away.
It might be perfectly safe to write code that bypasses those warnings
using a cast. However, one thing a cast cannot do for you is allow you
to safely change the contents of an object which is itself defined as
'const' (rather than, for instance, a pointer to 'const'). The behavior
is always undefined if your code attempts that,