c - Can't find error in string manipulation/pointers -


so first off, homework assignment, please don't write code me, point out code wrong.

the basics of code it's 'address/balance' book. i've got struct variables, reason, doubly linked list getting messed , can't figure out how. bit of fancy (not really) debugging, i've figured out think fgets(string, 200, file); line overwriting head->name pointer somehow, seems throw rest of code off. relevant code snippets here:

populating list:

void populate_list(char* filename){     file *file = null;     char* name = null;     char* streetaddress = null;     char* city = null;     char* state = null;     char line[200];     int zip;     float balance;      file = fopen(filename,"r");      if(file==null){         printf("invalid input file\n");         exit(1);     }     if(file == 0){         printf("invalid file.\n");         exit(1);     }      while(!feof(file)){          fgets(line, 200, file);          name = strtok(line, ",");         streetaddress = strtok(null, ",");         city = strtok(null,",");         state = strtok(null,",");         zip = atoi(strtok(null,","));         balance = atof(strtok(null,","));          strip_spaces(name);         strip_spaces(streetaddress);         strip_spaces(city);         strip_spaces(state);          add_node(name, streetaddress, city, state, zip, balance);      }      fclose(file);     return; } 

then add_node code:

void add_node(char* name, char* streetaddress, char* city, char* state, int zip, float, balance){     struct customer* addnode = null;      if(find_duplicate(name)){         print_filler(1);         printf("duplicate record: %s\n", name);         return;     } else {         addnode = (struct customer *) malloc(sizeof(struct customer));         addnode->name = name;         addnode->streetaddress = streetaddress;         addnode->city = city;         addnode->state = state;         addnode->zip = zip;         addnode->balance = balance;          if(head == null) {             head = addnode;             addnode->prev = null;         } else {             tail->next = addnode;             addnode->prev = tail;         }          tail = addnode;         addnode->next = null;     }      print_list();     return; } 

the bug seems happening after first time add_node called , happens when fgets() goes off second time. it's overwriting head->name entire fgets() line reason.

there other random irritating bugs haven't pinned down yet, 1 think may source of others.

full code here, if helps: http://pastebin.com/k0pqyvt0

my guess it's head->name pointer being overwritten fgets(), can't life of me figure out doing this, help/advice appreciated.

edit: solution implement strdup() , duplicate strings when they're passed add_node(). answers.

the main problem assigning members of node allocate point line buffer, , buffer overwritten every time call fgets() fill it. pointers in every node point same buffer (which lives on stack).

the solution (as mentioned in other answers) use strdup() allocate separate copy of each string you're saving each node member. work:

.... } else {     addnode = (struct customer *) malloc(sizeof(struct customer));     addnode->name = strdup(name);     addnode->streetaddress = strdup(streetaddress);     addnode->city = strdup(city);     addnode->state = strdup(state);     addnode->zip = strdup(zip);     addnode->balance = strdup(balance);     ... } 

it's idea zero-out contents of node after has been allocated, don't garbage in it:

   addnode = (struct customer *) malloc(sizeof(struct customer));    memset(addnode, '\0', sizeof(struct customer)); 

Comments