c代码文件崩溃,编译时没有错误

c code file is crashing and there is no error while compile

我在我的代码上做了所有正确的事情,但仍然有一个问题,我不知道为什么

#include<stdio.h>
#include<stdlib.h>
struct node { 
    int data ; 
    struct node* link ; 
};
struct node* top = NULL ;
void pop(){ 
    if(top==NULL) 
        printf("element that will delete is %d\n",top->data);
    else {
        struct node* temp ;
        temp= top ; 
        top = top->link ;
        temp->link = NULL ; 
        free(temp);
        }}
void push() { 
    struct node* temp ;
    temp =(struct node*) malloc(sizeof(struct node ));
    printf("please enter the data of the first node \n"); 
    scanf("%d",&temp->data);
    temp->link = top ; 
    temp = top ; 
} 
void printx(){
    if(top==NULL)
        printf("there is no element to print\n");
    else { 
        struct node* temp ; 
        while(temp != NULL) {
        printf("%d\n",temp->data) ;
        temp = temp->link ; 
        }


    }
}
int main() { 

    printf("please enter the type of operation u want to perform\n") ;
    printf("enter 1 for push , 2 for pop , 3 for print:  ") ;

    int x ; 
    scanf("%d",x) ; 
    switch(x){
        case 1 :    
            printf("\nu choosed push\n");
            push(); 
            break ; 

        case 2 :    
            printf("u choosed  pop \n"); 
            pop() ; 
            break ;

        case 3 :    
            printf("u choosed to print the element of the stack\n") ;
            printx(); 
            break ; 

    }

}

如果您将此代码粘贴到 https://www.onlinegdb.com/,然后单击 "Debug",然后在 gdb window 中键入 run,它会将您带到创建错误。 (您可以单击“调试”按钮附近的 "help" 按钮以获取有关如何进一步使用此工具的说明。或者您可以 google "gdb debugging" 或 "visual studio debugging" 以获得进一步的帮助。)

错误在您的 scanf 行。如果你 google "scanf",你可能会注意到 int 参数传递 address of int。所以你需要 &x,而不是 x。您的程序还有其他错误。我建议您使用上面提到的调试器帮助按钮,单步执行您的程序,并观察发生了什么。例如,您会注意到您的程序在解决第一个问题后立即退出。我想你会想要一个循环来防止这种情况发生。此外,在您的 pop 中,您专门检查是否 top==null,然后取消引用它 (top->data) 如果是。这保证是非法的。这是您可以通过 运行 调试器检查的内容,选择 pop,然后打印您的程序试图访问的值(在调试器中)(print top->data 在 gdb window 中)