I've just realized that something I've always assumed is illegal might not be.
Can you re-assign different string literals to a const char* this way?Code:const char* s = "okay"; [...] s = "something else"; [...] s = "and so on";
I've just realized that something I've always assumed is illegal might not be.
Can you re-assign different string literals to a const char* this way?Code:const char* s = "okay"; [...] s = "something else"; [...] s = "and so on";
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
Yeah absolutely!
You would only not be able to do that if you declared s as:
Code:const char*const s = "You're stuck with me";
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
The data *pointed at* by the pointer cannot be changed. But the data pointer can be changed to point to somewhere else.
Quite a common "gotcha", especially in functions that takes things like that as parameters. As iMalc says, if you declare both the pointer AND the data it points at as const, then it will fail as expected.
Huh. Well, good I guess, live and learn.
I understand the stuff about const pointer vs. const data. It just suddenly struck me that the literal itself might be compiled into the read-only data segment, and not stack (which would potentially take it out of scope). For whatever reason, I'd been thinking that only happened when a const char pointer is initialized with a literal.
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge