字符串赋值与初始化

字符串赋值与初始化

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空间是否足够

猜你喜欢

转载自blog.csdn.net/qq_38335172/article/details/78142652
今日推荐