質問があります。 が、私はこのコードを試してみて、受け取る "セグメンテーションフォールト" エラー:"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
私のミス?
あなたは[タグ:c]というタグを付けましたが、[タグ:C++]ヘッダーと 'using namespace std;'で書かれています。 CまたはC++を記述しますか? 1つを選択してそれに固執するのが最善です - 2つは以前よりも互換性がありません。 – sarnold
それは私の大学の運動、私はSTLを好む –