gpt4 book ai didi

concat char* 与 char netbea

转载 作者:行者123 更新时间:2023-11-30 17:38:28 29 4
gpt4 key购买 nike

我需要在c中连接一个字符串和一个字符,但我没有弄清楚。这是我的代码的一部分:

  unsigned char c ='d';
char *respuesta;

while(ciclo)
{
nanosleep((struct timespec[]){{0, INTERVAL_MS}}, NULL);
//veces++;

if (read(tty_fd,&c,1)>0)
{
write(STDOUT_FILENO,&c,1);
respuesta = append(respuesta,c);
}
else{ciclo = false;}
}

void append(char* s, char *c)
{
int len = strlen(s);
s[len] = c; // in this line I got the error.
s[len+1] = '\0';
}

提前致谢。

最佳答案

更改:

void append(char* s, char *c)

至:

void append(char* s, char c)

因为您想要将 char(单个字符)附加到 char *(字符串)。

还要确保您已经为 respuesta 分配了(足够的)内存 - 它只是上面代码中的一个野指针,但希望您实际上已经 malloc 了合适的数量存储。

关于concat char* 与 char netbea,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22121858/

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