gpt4 book ai didi

将 const char* 复制到 c 中的字符串

转载 作者:行者123 更新时间:2023-12-02 08:45:11 24 4
gpt4 key购买 nike

在 C [不是 C++] 中:

如何将 const char* 复制到字符串(字符数组)中?

我有:

const char *d="/home/aemara/move/folder"
char tar[80] ;
int dl=strlen(d);
int x=0;
while (x<dl){tar[x]=d[x]; x++; }
printf("tar[80]: %s\n",tar);

打印出来:tar[80]:/home/aemara/move/folderøèB问题是这样,在数组的末尾添加垃圾[有时,不总是]我该如何解决?或者还有另一种方法可以将 const char* 复制到字符串中?

最佳答案

strlen 返回没有空终止符的长度。您需要再复制一个字节。

关于将 const char* 复制到 c 中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13000910/

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