C - seg fault - cant find error -
C - seg fault - cant find error -
i writing programme (in c) implement self referential linked list. have written bit of code , got compile i'm getting segmentation fault , have no thought why. below code:
#include <stdio.h> #include <stdlib.h> #define true 1 #define false 0 struct node { int value ; struct node *next; }; void insert(int x, struct node **pl); void printlist(struct node *l); typedef int boolean; int main(int argc, char *argv[]) { int i; struct node *l; for(i = 3 ; < 20; i+= 2) insert(i,(&l)); printlist(l); homecoming 0; } void insert(int x, struct node **pl) { if((*pl) == null) { (*pl) = malloc(sizeof(struct node)); (*pl)->value = x; (*pl)->next = null; } else { insert(x, &((*pl)->next)); } } void printlist(struct node *l) { printf("%d\n", (l)->value); if(((l)->next) != null) { printlist((l)->next); } }
you don't initialize l , utilize in insert via *pl. try:
struct node *l = null; c segmentation-fault
Comments
Post a Comment