我刚刚开始在学校用 C 语言编程。我被要求编写一个使用 FIFO 结构来解决数学问题的程序。我在互联网上获得了以下 FIFO 代码,我只是不知道如何使用它。我尝试了很多东西,但在互联网上找不到任何有用的东西,或者也许我只是不知道研究正确的东西,但你能帮助我吗?谢谢!
#include <stdio.h>
#include <stdlib.h>
typedef struct pile
{
int donnee;
struct pile *precedent;
} Pile;
void pile_push(Pile **p_pile, int donnee)
{
Pile *p_nouveau = malloc(sizeof *p_nouveau);
if (p_nouveau != NULL)
{
p_nouveau->donnee = donnee;
p_nouveau->precedent = *p_pile;
*p_pile = p_nouveau;
}
}
int pile_pop(Pile **p_pile)
{
int ret = -1;
if (p_pile != NULL)
{
Pile *temporaire = (*p_pile)->precedent;
ret = (*p_pile)->donnee;
free(*p_pile), *p_pile = NULL;
*p_pile = temporaire;
}
return ret;
}
void pile_clear(Pile **p_pile)
{
while (*p_pile != NULL)
{
pile_pop(p_pile);
}
}
我尝试这样做:
int main()
{
int return_val;
Pile pile;
pile_push(Pile, 5);
return_val = pile_pop(Pile);
printf(return_val);
}
并收到此错误:
expected expression before 'Pile'
too few arguments to function 'pile_push'
最佳答案
您混淆了 Pile
和 pile
,这是第一个警告的问题。这些函数需要一个指向 Pile 的指针。也就是说:它们更新指针的值,因此需要向它们传递对指针的引用。您对 printf 的使用也是错误的。
int main()
{
int return_val;
Pile *pile = NULL;
pile_push(&pile,5);
return_val = pile_pop(&pile);
printf("return_val is: %d\n",return_val);
}
关于c - 如何将 typedef 结构用于 FIFO?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40323804/