原帖由 caijimin 于 2008-4-7 16:31 发表
#include
int main()
{
char *t = "Hello world!";
printf("%s\n",t);
return 0;
}
# g++ a.c
a.c: In function 'int main()':
a.c:5: warning: deprecated conv ...
#include <stdio.h> int main() { char *a= "Hello world!"; char *t = (char*)a; printf("%s\n",t); return 0; } |
原帖由 caijimin 于 2008-4-7 16:31 发表
#include
int main()
{
char *t = "Hello world!";
printf("%s\n",t);
return 0;
}
# g++ a.c
a.c: In function 'int main()':
a.c:5: warning: deprecated conversion from string constant to 'char*'
原帖由 mymtom 于 2008-4-7 18:09 发表
这样写本来就可能出问题:
char *t = "Hello world!";
改为:
const char *t = "Hello world!";
是正道.
这样可以防止:
t[2] = 'H';
或者:
strcpy(t, "World");
之类的错误.
欢迎光临 Chinaunix (http://bbs.chinaunix.net/) | Powered by Discuz! X3.2 |