OUTPUT segmentation fault C code Code: #include<stdio.h> #define OK 1 #define ERROR 0 typedef struct node{ int data; struct node *next; }node,*list; int insertionattail(list l,int data) { list t,temp; temp=l; t=(list)malloc(sizeof(node)); if(t==NULL)return ERROR; t->data=data; while(l->next) { l->next=l->next->next; } t->next=NULL; l->next=t; l=temp; return OK; } int main() { node n; list l; l=&n; n.next=NULL; insertionattail(l,5); insertionattail(l,7); printf("data1=%d,data2=%d",l->next->next->data,l->next->next->next->data); return 0; }
This is what you get when you explicitly cast pointer returned by malloc. To call this function an appropriate header needs to be included - <stdlib.h>. Without the explicit cast the complier would display a warning.