为什么这段代码不起作用?C++

Sam*_*Paz -3 c++ pointers memory-management

我正在学习cpp并需要一些帮助.我的代码无效,它停止了add->value = value.

typedef struct node node;
struct node{
    int value;
    struct node *next;  
};

node *top; 

int insert(int value){
    struct node *add;
    cout<< "here it stops";
    add->value = value;
    add->next = NULL;

    if(top == NULL ){
        top == add;
    }else{
        add->next = top;
        top =  add;
    }  
}
Run Code Online (Sandbox Code Playgroud)

jua*_*nza 7

当你这样做:

struct node *add;
Run Code Online (Sandbox Code Playgroud)

你正在定义一个指针node(你可以省略struct这里).但指针不指向任何有效node对象.node在完成此操作之前,您必须指向a :

add->value = value;
Run Code Online (Sandbox Code Playgroud)