c - #define a string literal then assign it to a char* -
#define maxstr "maximum number reached" char *str = maxstr;
while doing kind of operation. code working & running fine getting lint error. how can resolve it?
error: assignment of string literal variable
if use:
#define maxstr "maximum number reached" char *str = (char *) maxstr;
then lint error:
attempt cast away const (or volatile)
assignment of string literal variable
that horrible error message. i'm curious lint thinks string literals for, if cannot assign them variables... should like: "assigning string literal non-constant pointer".
attempt cast away const (or volatile)
the warning incorrect. again, should tell pointer variable needs const
.
to sum up, errors because static analyser tool bad.
explanation:
string literals in c character arrays, char []
. unfortunately not treated constant type const char[]
language. defect in c language, because if attempt write access of string literal, leads undefined behaviour , program might crash , burn. must treat string literals if const arrays, though not.
therefore, practice declare pointers string literals const char*
.
in case of lint, seems treat string literals if const char[]
, not. therefore gives incorrect errors instead of pointing out actual problem.
Comments
Post a Comment