Assume the following code in C
:
const char * string = "123";
if(condition) string = "somethingelse";
printf("%s\n", string);
Although I know that it is working (at least with gcc
on x86_64
) and no compiler warning are being generated, I'm wondering if this is something I should be doing like this or if there is a better alternative.
Note that this question is not a duplicate of Difference between char* and const char*? as it explicitly asks if a redefinition of the content of the variable is good practice. The other question is asking for the difference between char*
and const char*
and the accepted answer does only provide the answer if one already knows what the assignment of another value means. This was unclear to me and is an essential of this question.