It generally means you
shouldn't change what is pointed to.
#include <stdio.h>
const char *foo(const char *text)
{
puts(text);
return text;
}
int main(void)
{
const char literal[] = "non-modifiable string literal";
char *oops = foo(literal);
oops[0] = 'N'; /* BAD! */
return 0;
}
Here the a C++ compiler will prevent you from assigning the return value of foo to oops. But a C compiler may just give you a warning, allowing you to do bad things. Namely, a string literal may be put in program code (or somewhere else that is read-only), and should not be considered to be writable. But the languages do afford you ways to do things you shouldn't.
By const-qualifying the return value, the programmer who gave you the protoype is providing a damn good hint to prevent you from doing things you ought not do. Warnings and errors should then hopefully guide you into writing correct code.
But there is generally no foolproof way of stopping a programmer from doing dumb things and writing code that crashes or otherwise does bad things.
Reputation Points: 2780
Solved Threads: 312
long time no c
Offline 4,790 posts
since Apr 2004