?
=?iso-8859-1?q?Santiago_Urue=F1a?=
Hi,
I tried to return a pointer to a constant string, but the compiler
gives the following warning if a cast is not used:
warning: assignment from incompatible pointer type
This is the code:
const char msg[] = "Test message";
const char *message(void) {
return msg;
}
int main(void){
const char * str;
str = (const char *)message;
str = (char *)message;
str = message; /* GCC warning! */
str = (const char *)msg;
str = (char *)msg;
str = msg;
return 0;
}
Oddly, GCC only gives the warning if no cast is used, but it doesn't
complain if the cast discards the const qualifier. Is this behavior
OK? I'm using GCC 4.1.2. Thanks!
Best regards,
Santi
I tried to return a pointer to a constant string, but the compiler
gives the following warning if a cast is not used:
warning: assignment from incompatible pointer type
This is the code:
const char msg[] = "Test message";
const char *message(void) {
return msg;
}
int main(void){
const char * str;
str = (const char *)message;
str = (char *)message;
str = message; /* GCC warning! */
str = (const char *)msg;
str = (char *)msg;
str = msg;
return 0;
}
Oddly, GCC only gives the warning if no cast is used, but it doesn't
complain if the cast discards the const qualifier. Is this behavior
OK? I'm using GCC 4.1.2. Thanks!
Best regards,
Santi