What is the difference between const char*
and static const char*
in C?
I think that the answer of Difference between static const char* and const char* is wrong.
Indeed, const char*
elements are put in the .rodata
section of the program, otherwise the following would lead to a segfault:
const char* f() {
const char* hello = "hello";
return hello;
}
int main() {
const char* hello_after = f();
printf("%s\n", hello_after);
}
Indeed, because that code works, the pointer returned by f
is still pointing to alive data, which shows that that data is not allocated on the stack but stored in .rodata
.
But then, it seems to me that const char*
and static const char*
are the same things as far GCC is concerned.
But then, why the behavior not the same for const int*
and static const int*
? Is that an exception, hardcoded in GCC, that only for the type char
then const
and static const
should be the same?
Thank you very much for your help!