我有一些問題。 我試試這個代碼,並收到 「分段故障」 錯誤:沒有「const char *」的分段錯誤
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
struct Stack {
int value;
Stack *next;
};
void push(Stack* top, int value) {
Stack *ntop = new Stack;
ntop->value = top->value;
ntop->next = top->next;
top->next = ntop;
top->value = value;
}
int pop(Stack* top) {
int val = top->value;
top->value = top->next->value;
top->next = top->next->next;
return val;
}
int main()
{
Stack *top;
top->next = NULL;
push(top, 20);
cout << pop(top);
}
[10:40:46] [~] >> g++ 3.cpp -o 3 && ./3 Segmentation fault
,但如果我添加爲const char *測試= 「」;之前Stack * top;它工作正常:
int main()
{
const char* test = "";
Stack *top;
top->next = NULL;
push(top, 20);
cout << pop(top);
}
[10:47:33] [~] >> g++ 3.cpp -o 3 && ./3 20
在我的錯誤呢?
您已經標記了[tag:c],但是已經用[tag:C++]標題和'using namespace std;'編寫 - 這是什麼?你想寫C或C++嗎?最好選擇一個並堅持下去 - 這兩個比以前更少互換。 – sarnold 2011-12-24 08:53:33
它只是我的大學鍛鍊,我更喜歡STL – 2011-12-24 08:58:37