我有一些问题。 我试试这个代码,并收到 “分段故障” 错误:没有“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