代码在循环的第二次运行时停止

标签 c pointers data-structures while-loop scanf

我无法运行此代码以提供两个输入。它在运行时突然停止。代码如下。请帮我修复它。

进程返回-1073741819 (0xC0000005) 按任意键继续。是错误消息

#include <stdio.h>
#include<malloc.h>
#include<conio.h>
struct coordinates
{
    int x;
    int y;
    struct coordinates *link;
};
void append(struct coordinates **q,int xx , int yy)
{
    struct coordinates *r,*s;

    if(*q == NULL)
    {
        r = (struct coordinates *)malloc(sizeof(struct coordinates));
        r->x=xx;
        r->y=yy;
        *q=r;
    }

    else
    {
        r=*q;
        while(r->link != NULL)
        r= r->link;

        s=(struct coordinates *)malloc(sizeof(struct coordinates));
        s->x=xx;
        s->y=yy;
        s->link=NULL;

        r->link=s;

    }
}
void display(struct coordinates *temp)
{
    while (temp  != NULL)
    {
        printf("x coordinate is %d ,Y coordinate is %d",temp->x,temp->y);
        temp=temp->link;
    }
}
int main()
{
    struct coordinates *start;
    start=NULL;

    char name;
    int xxx,yyy;

    while(1)
    {
        printf("If you want to continue input loop press y \n");
        scanf(" %c", &name);
        if (name == 'y')
        {
            printf("enter x coordinate of element \n");
            scanf("%d",&xxx);
            printf("%d\n",xxx);

            printf("enter y coordinate of element \n");
            scanf("%d",&yyy);
            printf("%d\n",yyy);

            append(&start,xxx,yyy);

        }
        else
        {
            printf("You have exited input loop \n");
            break;
        }

    }
    display(start);
}

最佳答案

成功

r->x = xx;
r->y = yy;
r->link = NULL;

在追加的第一个分支中,否则第二次在列表末尾r->link有未指定的值(不太可能是NULL)。

关于代码在循环的第二次运行时停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27458860/

相关文章:

python - 如何在 Cython 中声明带有指针的链表

arrays - 确定是否存在长度 > 1 且均值为整数的连续子数组

谁能检查程序并告诉我如何获得正确的输出?

c - C 中的 Visual Studio 错误,0xfefefefe 处未处理的异常

c - 将指针A分配给B。释放A后,B仍然存在

c++ - 如何实现缓存友好的动态二叉树?

c++ - 交换不同大小的数组

将简单的 C#define 转换为 Rust 常量

c - 编写读取 CFG 并删除左递归的解析器的建议

c - C 中的指针和数组有什么不同吗?