gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 20:51:40 24 4
gpt4 key购买 nike

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

进程返回-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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com