gpt4 book ai didi

c - 为什么在调用strcat时添加\001

转载 作者:太空宇宙 更新时间:2023-11-04 01:34:34 26 4
gpt4 key购买 nike

看下面的代码:

char chs[100] = "Hello World";
char token[100];
int pos = -1;
while((current = chs[++pos]) != '"'){
strcat(token, &current);
}

但是输出是:

H\001e\001l\001l\001o\001 \001W\001o\001r\001l\001d

有什么想法吗?

最佳答案

strcat() 在输入时需要一个以 null 结尾的字符串。所以 strcat(token, &current) 将从 current 的地址开始读取并继续读取,直到找到 null。只是碰巧,在 current 之后你在内存中的内容是“\001”,所以每次你执行 strcat 时,它都会将所有内容复制到 token 中。

你应该做 char current[] = "\0\0"然后用 current[0] = chs[++pos] 赋值。这样电流将始终具有空终止。

关于c - 为什么在调用strcat时添加\001,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16755552/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com