...
Making code reverse compatible to fit this standard sometimes breaks functionality but this is a good recommendation to follow on new code. An example of a situation where implementing this would break prior code is if a string literal is assigned to a non-const pointer as in the following example
Before changing string literals to constant pointers
| Code Block | ||
|---|---|---|
| ||
char* CMUfullname = "Carnegie Mellon";
...
//take user input to determine string variable "school"
if(strcmp(school,"CMU"))
{
school=CMUfullname;
}
|
...
If one were to simply change the declaration of the string literal "CMUfullname" to const this would be the output
| Code Block | ||
|---|---|---|
| ||
const char* CMUfullname = "Carnegie Mellon";
...
//take user input to determine string variable "school"
...
if(strcmp(school,"CMU")==0)
{
school=CMUfullname;
}
|
...