字符串赋值与初始化

char cString[10]="hello world" 是初始化,合法。

 

char cString[10];

cString="hello world" 是赋值,这样赋值非法。原因在于声明了cString数组后,cString其实是一个char型的常量指针,而cString="hello world" 一句的意思是将常量"hello world"的首地址赋给cString,这与cString指针的常量属性冲突。

可以用strcpy(cString, "hello world")的方法来赋值,但要注意检查cString空间是否足够

posted @ 2012-05-15 13:21  children  阅读(6162)  评论(0编辑  收藏  举报