问题描述
嗨
这是正确的
char teststr [100] =" \0" ;;
或是否有其他方法可以初始化;
谢谢
le
-
通过Mailgate发布。 ORG服务器 -
推荐答案
char teststr [100] =""足够了,因为字符串文字在结尾处已经有一个
NUL字符(在这种情况下位于0)。
Christian
char teststr[100] = "" suffices, because the string literal already has a
NUL character at the end (in position 0, in this case).
Christian
以下两者做同样的事情,或许更优雅的触摸:
char teststr [100] ="" ;;
char teststr [100] = {0 };
The following both do the same thing, perhaps a touch more elegantly:
char teststr[100] = "";
char teststr[100] = {0};
以下两者都做同样的事情,也许是更优雅的触摸:
char teststr [100] =" " ;;
The following both do the same thing, perhaps a touch more elegantly:
char teststr[100] = "";
这会将第一个元素初始化为终止空字符,并且
对数组的其余部分不执行任何操作。 char teststr [100] = {0};
This initializes the first element to a terminating null character and
does nothing to the rest of the array. char teststr[100] = {0};
这个不做同样的事情。这个将整个
数组初始化为零。
This one does not do the same thing. This one initializes the entire
array to zeros.
这篇关于初始化char字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!