2014-12-13 3 views
-3

Я не могу запустить этот код, чтобы дать два входа. Он резко останавливается во время выполнения. Код выглядит следующим образом. Пожалуйста, помогите мне в ее исправлении.Код останавливается во втором прогоне цикла

Возвращенный процесс -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); 
} 

ответ

3

Сделать это

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

в первой ветви Append, в противном случае r->link имеет неопределенное значение (вряд ли будет NULL) в конце списка второй раз вокруг.

Смежные вопросы