gpt4 book ai didi

c - realloc 误解

转载 作者:太空宇宙 更新时间:2023-11-04 06:40:13 27 4
gpt4 key购买 nike

有人可以向我解释一下,为什么这段代码不起作用。我正在浏览周围的一些问题,但找不到答案。可能是因为(大量)缺乏知识。

感谢您提供的任何帮助。

char** sentence = malloc(min);
char* temp = malloc(min2);
int i = 0;

while(i<5)
{
sentence = realloc(sentence, i+2);
scanf("%s", temp);
sentence[i] = malloc(strlen(temp));
strcpy(sentence[i], temp);
printf("%s\n", sentence[i]);
i++;
}

最佳答案

您忘记考虑字符串具有空终止符这一事实。

关于c - realloc 误解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9611185/

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