這是初學者所犯的經典錯誤。
讓我們來看看你的主要功能:
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