c - Node in linked list appears to be empty when trying to print it -
so, here thing. have program stores different authors in linked list, each author has own records , each record holds texts, organized in linked list. data read file. here code:
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> struct authorrecord { char texttitle[100]; int numberofwords; long download; struct authorrecord *next; }; typedef struct authorrecord *authorrecordtype; typedef struct { char firstname[30]; char lastname[30]; int idnumber; int textnum; authorrecordtype text; } authortype; struct membernodestruct { authortype *anauthor; struct membernodestruct *next; }; typedef struct membernodestruct *membernodetype; file* input; file* output; membernodetype head; membernodetype member; int main (void) { int authornum, textnum, i, j; member = malloc(sizeof(struct membernodestruct)); head = member; member->next = null; input = fopen("input.txt", "r"); if (input == null) { printf("could not open file.\n"); return 1; } fscanf(input, "%d", &authornum); (i = 0; < authornum; i++) { member->anauthor = malloc(sizeof(authortype)); textnum = 0; if(fscanf(input, "%s %s %d", member->anauthor->lastname, member->anauthor->firstname, &member->anauthor->idnumber) != eof) { fscanf(input, "%d", &member->anauthor->textnum); (j = 0; j < member->anauthor->textnum; j++) { member->anauthor->text = malloc(sizeof(struct authorrecord)); fscanf(input, " %[^\n]", member->anauthor->text->texttitle); fscanf(input, "%ld", &member->anauthor->text->download); member->anauthor->text = member->anauthor->text->next; } member->next = malloc(sizeof(membernodetype)); member = member->next; } else member->next = null; } member = head; while(true) { if (member->next == null) break; (i = 0; < authornum; i++) { printf("%s %s %d\n", member->anauthor->lastname, member->anauthor->firstname, member->anauthor->idnumber); (j = 0; j < member->anauthor->textnum; j++) { printf("%s\n", member->anauthor->text->texttitle); printf("%ld\n", member->anauthor->text->download); member->anauthor->text = member->anauthor->text->next; } member = member->next; } } }
i have run program through gdb , read fine. however, when line printf("%s\n", member->anauthor->text->texttitle);
segfault. apparently member->anauthor->text
field empty, address 0x0. data , memory addresses on member
, member->anauthor
correct.
what wrong here?
when read texts each author, use text
member of autor node iterator
(j = 0; j < member->anauthor->textnum; j++) { member->anauthor->text = malloc(sizeof(struct authorrecord)); fscanf(input, " %[^\n]", member->anauthor->text->texttitle); fscanf(input, "%ld", &member->anauthor->text->download); member->anauthor->text = member->anauthor->text->next; }
the logic of whole block wrong. text->next
never has meaningful value, not null
. , texts not linked, unrelated chunks of data, once loop has been traversed unreachable. in particular, must allocate first, set previous link's next
pointer memory.
the text
pointer of text list should head
in member list , should assigned once. (after initialisation null
, is.)
that said, there various "smells" in code:
- the structures horribly named. know it's assignment , given structures starting point, why text nodes called
authorrecord
when differentautortype
? - as has been pointed out, defining pointer types structs adds nothing; confuses, espectally if types named
authorrecordtype
, notauthorptr
orpauthor
. in c, star marks pointer type; it's hard more expressive that. (plus, it's easier type.) - keeping track of number of entries in list member can useful, traversal of list should list structure (i.e.
next
pointers) avoid possible incosistencies. - when add elements list, call
malloc
when need memory, not in advance. if malloc first element before reading , setnull
because turns out didn't need afer all, create memory leak. - every
malloc
needsfree
after you're done. (i know code isn't complete @ moment; saying.) - your whole
scanf
sequence needs error checking. it's easy infinite loop if input not correct. (side note: it's helpful post short snippet of example input here find error.) - consider making small functions creating each of types, cleaning them , adding them lists; don't put
main
. idea separate reading creating structures. - you use long sequences of member references this:
member->anauthor->text->next
. consider using temporary pointers. make references shorter (text->next
) , code cleaner.
Comments
Post a Comment