I have some code:
int main() {
const char *string1 = "foo";
char *string2 = "bar";
string1 = string2;
return 0;
}
When I build it, no warnings are raised. However, with
int main() {
const char *string1 = "foo";
char *string2 = "bar";
string2 = string1;
return 0;
}
A warning is raised:
warning: assignment discards 'const' qualifier from pointer target type [-Wdiscarded-qualifiers]
string2 = string1;
^
With my current basic knowledge of C, I would have thought that string1 would not be reassignable since it is const. To my surprise, it was the code in the first snippet that built with no warnings and the second code snippet the opposite.
Any explanations of what is going on? Thanks!