Closed. This question is off-topic。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗? Update the question,所以它是on-topic,用于堆栈溢出。
                        
                        6年前关闭。
                                                                                            
                
        
struct node{
int data;
struct node *next;
};

struct node *head,*temp;

void insert()
{
   struct node *var;
   head=NULL;
   var=(struct node*)malloc(sizeof(struct node));
   printf("enter the data:");
   scanf("%d",var->data);
   temp=head;
   if(head==NULL)
   {
     head=var;
     head->next=NULL;
   }
   else
   {
     while(temp->next!=NULL)
     {
       temp=temp->next;
     }
     if(temp->next==NULL)
     {
       temp->next=var;
       temp=temp->next;
       temp->next=NULL;
     }
   }
 }

 void display()
 {
     temp=head;
     if(temp==NULL)
     {
        printf("empty list");
     }
     while(temp->next!=NULL)
     {
       printf("%d",temp->data);
       temp=temp->next;
     }
 }

 void main()
 {
     int value,choice;
     printf("\nenter choice:");
     scanf("%d",&choice);
     while(choice==1)
     {
        insert();
        display();
        printf("\nenter choice:");
        scanf("%d",&choice);
     }
     getch();
 }


我已经使用c制作了一个链接列表,但是代码未显示该链接列表,而是将空指针编译显示为输出,如何解决该问题,我是c编码的新手,因此无法找到适当的解决方案?

最佳答案

scanf("%d",var->data);
//--> scanf("%d",&(var->data));


scanf的参数必须是指针类型。

09-13 04:25