Possible Duplicate:
String literals vs const char* in C
Why i can do this?
void foo(char * cstr) {
...
}
and in code
foo("some text");
shouldnt “some text” be of type const char * ?
if its char *, means i can modify it?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Yes, it should be
const.In C++03, you are allowed to omit the
constonly for backward compatibility with C, but with two caveats:You are still not allowed to modify the data. Yes, this is highly confusing. That’s why leaving out
constis deprecated. Ideally it would be plain disallowed (and, in C++11 onwards, it is).If you have your compiler’s warning level set properly, you will be warned when trying to do this. If you have your compiler’s error level set really strictly then it will be treated as an error.