c - Linked list within a structure -


i new c , trying work out logic program working on in small test app.

what purpose read values database add data structure, within structure contain linked list other values related top level structure. seem adding things fine structure , linked list when try , retrieve values crashes.

below definition of structures

typedef struct calllogstructure {     char * date;     char * time;     char * bparty;     char * aparty;     float duration;     char * cleardowncause;     struct node *outboundlegs; } calllogstructure;  typedef struct node {     char * target;     float targetduration;     char * targetcleardowncause;     struct node *next; }node; 

below how initialising structures , calling method add data linked list.

char *outboundtarget = "0";     float outboundduration = 0;     char *outboundcleardown = "0";      calllogstructure * calllog = null;     node *temp = null;     int datarow = 0;      calllog = malloc(datarow+1 * sizeof(calllog));     //start = (node*)malloc(sizeof(node));     calllog[0].outboundlegs = null;     calllog[0].outboundlegs = (node*)malloc(sizeof(node));     if (calllog[0].outboundlegs == null)     {         printf("failed allocate ram\n");     }     temp = &calllog[0].outboundlegs;     temp->next = null;     calllog[0].outboundlegs->target = "0";     calllog[0].outboundlegs->targetduration = 0;     calllog[0].outboundlegs->targetcleardowncause = "0";      //insert first inbound leg     calllog[0].date = "16/05/2011";     calllog[0].time = "00:00:03";     calllog[0].aparty = "12345";     calllog[0].bparty = "67890";     calllog[0].duration = 0;     calllog[0].cleardowncause = "unanswered";      outboundtarget = "98765";     outboundduration = 0;     outboundcleardown = "unanswered";      insertoutboundleg(&calllog[0].outboundlegs, outboundtarget, outboundduration, outboundcleardown);       printf("newly inserted outbound target: %s", calllog[0].outboundlegs->target); //this it's crashing. 

below insertoutboundleg function

void insertoutboundleg(struct node *pointer, char * target, float targetduration, char * targetcleardowncause) {     if (pointer->target == null)     {         asprintf(&pointer->target, "%s", target);         pointer->targetduration = targetduration;         asprintf(&pointer->targetcleardowncause, "%s", targetcleardowncause);         //pointer->target = target;         //pointer->targetduration = targetduration;         //pointer->targetcleardowncause = targetcleardowncause;     }     else     {         while (pointer->next != null)         {             pointer = pointer->next;         }         pointer->next = (node *)malloc(sizeof(node));         pointer = pointer->next;         //pointer->target = target;         //pointer->targetduration = targetduration;         //pointer->targetcleardowncause = targetcleardowncause;         asprintf(&pointer->target, "%s", target);         pointer->targetduration = targetduration;         asprintf(&pointer->targetcleardowncause, "%s", targetcleardowncause);         pointer->next = null;     } } 

the idea when built, structure, along linked list contained within structure passed separate function export data file, have tried first printing values of outboundlegs (linked list) crashes, however, values top level structure (calllog) fine.

thanks can provide.

there multiple issues, start with

calllog = malloc(datarow+1 * sizeof(calllog)); 

change to

calllog = malloc(datarow+1 * sizeof(*calllog)); 

either initialize calllog[0].outboundlegs 0 memset(calllog[0].outboundlegs, 0, sizeof(*calllog[0].outboundlegs)) or use calloc()

calllog[0].outboundlegs = calloc(1, sizeof(node)); 

calllog[0].outboundlegs->target = "0"; 

don't initialize strings way, do

calllog[0].outboundlegs->target = strdup("0"); 

however, remember free memory when appropriate.


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -