这是初学者所犯的经典错误。
让我们来看看你的主要功能:
int main()
{
Pilha* Stack; // This line is your problem!
create_empty_stack(Stack);
}
如果你还记得的指针,声明Pilha* Stack;
使得堆栈是内存指针。但是现在它并没有指向任何东西,因为你没有为Pilha类型的对象预留内存!
你的程序崩溃是因为create_empty_stack()试图访问下一个,该对象的成员(请记住,该对象仍然不存在)。
所以,你应该做的却是:
int main()
{
// Reserve space in memory for one Pilha object and
// make Stack point to this memory address.
Pilha* Stack = (Pilha*) malloc(sizeof(Pilha));
create_empty_stack(Stack);
}
还是一个更简单的方法:
int main()
{
Pilha Stack; // Declare a new Pilha object
// and pass the memory address of this new object to create_empty_stack()
create_empty_stack(&Stack);
}
闻起来像功课。 – 2010-09-09 03:10:27
这是一个堆栈还是一个列表? – dreamlax 2010-09-09 03:30:38
@dreamlax:它是一个实现为单链表的堆栈。 – JeremyP 2010-09-09 10:52:12