初始化char字符串 [英] initialize char string
问题描述
嗨
这是正确的
char teststr [100] =" \0" ;;
或是否有其他方法可以初始化;
谢谢
le
-
通过Mailgate发布。 ORG服务器 - http://www.Mailgate.ORG
推荐答案
杨利写道:
Yang Lee wrote:
嗨
这是正确的
char teststr [100] =" \0" ;;
或是否有另一种初始化方法;
hi
is this correct
char teststr[100]="\0";
or is there another method to initialise;
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
Yang Lee写道:
Yang Lee wrote:
你好
这是正确的
char teststr [100] =" \0" ;
它有效。
还是有其他方法可以初始化;
hi
is this correct
char teststr[100]="\0";
It works.
or is there another method to initialise;
以下两者做同样的事情,或许更优雅的触摸:
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};
infobahn写道:
infobahn wrote:
Yang Lee写道:
Yang Lee wrote:
嗨
这是正确的
char teststr [100] =" \0" ;;
它的工作原理。
hi
is this correct
char teststr[100]="\0";
It works.
或者是否有另一种初始化方法;
or is there another method to initialise;
以下两者都做同样的事情,也许是更优雅的触摸:
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字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!