+ 3

What's the error in this C Program of Linked List?

#include<stdio.h> #include<conio.h> struct node { int info; struct node *next; }; int main() { struct node *head,*p,*q,*r; p->info=5; p->next=NULL; q->info=9; q->next=NULL; r->info=7; r->next=NULL; p->next=q; q->next=r; head=p; while(head!='\0') { printf("%d",head->info); head = head->next;} return 0; }

3rd Jan 2018, 2:57 PM
Rishabh
Rishabh - avatar
2 Answers
+ 1
/* @vukan,kurwius the above soln is given by my college professor but what was the logic behind this ...can anyone explain this ??? */ /* 😱 */ #include<stdio.h> #include<conio.h> struct node { int info; struct node *next; }; int main() { struct node *head,a,b,c,*p,*q,*r; p=&a; q=&b; r=&c; p->info=5; p->next=NULL; q->info=9; q->next=NULL; r->info=7; r->next=NULL; p->next=q; q->next=r; head=p; while(head!='\0') { printf("%d",head->info); head = head->next;} return 0; }
4th Jan 2018, 9:24 AM
Rishabh
Rishabh - avatar
+ 3
i think that you must initizale head to something before give it value of p
3rd Jan 2018, 4:55 PM
Vukan
Vukan - avatar