为什么 printf 只打印该数组的第一个字符而不是全部?
char *member[8];
char *tag;// this points a lot of text. if i print htmtag, it works fine with a few lines of text printing out
for ( int x = 0; x < 8; x++ )
{
member[x] = tag[x+15];
}
printf("member: %s",member);
如果我在 member[0]/member[1]/member[2] 上 printf ,值存储在那里,但打印以下内容,为什么上面只打印出 member 中的第一个字符而不是整个 7?
即
member: 1
和我想打印出来
member: 1234567
char *member[8];
char *tag;// this points a lot of text. if i print htmtag, it works fine with a few lines of text printing out
for ( int x = 0; x < 8; x++ )
{
member[x] = tag[x+15];
}
printf("member: %s",member);
Why would the above only print out the first char in member and not the whole 7, if i printf on member[0]/member[1]/member[2] the values are stored there but the following is printed?
i.e
member: 1
and i wanted to print out
member: 1234567
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
成员应声明为:
不是
字符数组,不是字符指针数组
member should be declared as:
not
as it is an array of chars, not an array of char pointers
由于您没有发布足够的代码,因此以下是猜测:
您的
member
变量不是char[]
而是wchar_t[]
或 MSTCHAR[]
。这将使member
数组的每个元素都大于 1 个字符,因此当printf
将其视为 char 数组时,它会看到'1', ' \0'、'2'、'\0'
等 - 打印第一个1
,然后在 0 字节处停止。As you didn't post enough code, the following is a guess:
Your
member
variable is not achar[]
but awchar_t[]
or MSTCHAR[]
. That would make each element of themember
array larger than 1 char, so whenprintf
treats it as an array of char it'd see'1', '\0', '2', '\0'
and so on - printing the first1
then stopping on the 0-byte.