如果我运行这个程序,即使我将输入设为0,它会不断获取其中的值?

时间:2013-08-15 14:55:55

标签: c

#include<stdio.h>
#include<conio.h>
#include<malloc.h>

void main()
{
    struct node
    {
        int data;
        struct node *next;
    };
    struct node *head,*temp;
    int x;
    clrscr();
    head=(struct node *) malloc (sizeof(struct node));
    temp=head;
    while(temp!=NULL)
    {
        scanf("%d",x);
        temp->data=x;
        if(x==0)
        {temp->next=NULL;}
        else
        {temp->next=(struct node *) malloc (sizeof(struct node));}
        temp=temp->next;
    }
}

我正在为一个简单的链接列表程序编写代码...我可以成功运行程序但是当我按0时程序没有停止..

1 个答案:

答案 0 :(得分:5)

首先,你的scanf是错误的。它需要通过引用传递:

scanf("%d", &x);

其次,在扫描之前你应该将x设置为0以外的值,以防万一。它的编写方式,循环中没有退出条件。

如果你想直接进入它,你可以尝试使用gdb并逐行踩它。

希望这有帮助