This is how you can:
const char *str = "ABCD"; But this is probably impossible?
void exmpl(const char *str) { str = "ABCD"; } const char *str; exmpl(str); This is how you can:
const char *str = "ABCD"; But this is probably impossible?
void exmpl(const char *str) { str = "ABCD"; } const char *str; exmpl(str); It is possible both. All literal strings are stored forever.
But the code is still a bug. To change the value in the called code, you need another pointer level:
void exmpl(const char **str){ *str = "ABCD"; } const char *str; exmpl(&str); The const char *str is a pointer to a constant string , so such assignments are quite acceptable. Now, if you had a declaration for const char * const str , then you would have a constant pointer to a constant string , which cannot be changed after initialization, so only the first option would pass.
Source: https://ru.stackoverflow.com/questions/522570/
All Articles