gpt4 book ai didi

c - 一堆字符串

转载 作者:行者123 更新时间:2023-11-30 20:49:15 25 4
gpt4 key购买 nike

我正在尝试实现一个包含字符串的堆栈,我想我现在已经非常接近了但我真的不知道为什么它不起作用

编辑:类似的事情?我现在已经接近了吗?

int pop(char **x)
{
if (isEmpty())
return 0;
else
{
struct Node *temp = head;
*x = head->wyraz;
head = head->next;
free(temp);
return 1;
}
}


int main()
{
char buf[255];
char *str;
while (fgets(buf, sizeof(buf), stdin) != 0 && buf[0] != '\n')
{

push(buf);
}



while (pop(&str) != 0)
{
printf("%s\n", str);
free(str);
}

return 0;
}

最佳答案

我看到的第一个主要问题是,您只需将指针存储到堆栈中,并且该指针始终设置为buf并不断被覆盖。

您应该strdup字符串以确保每个节点获得唯一的缓冲区(并记住在弹出它后的某个时刻释放它)。

关于c - 一堆字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21961410/

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