c++ - Is it bad to declare a C-style string without const? If so, why? -
doing in c++
char* cool = "cool";
compiles fine, gives me warning:
deprecated conversion string constant char*.
i never willfully use c-style string on std::string
, in case i'm asked question:
is bad practice declare c-style string without const
modifier? if so, why?
yes, declaration bad practice, because allows many ways of accidentally provoking undefined behavior writing string literal, including:
cool[0] = 'k'; strcpy(cool, "oops");
on other hand, fine, since allocates non-const array of chars:
char cool[] = "cool";
Comments
Post a Comment