Note that there are some explanatory texts on larger screens.

plurals
  1. POCan't find error in string manipulation/pointers
    text
    copied!<p>So first off, this is a homework assignment, so please don't write any code for me, just point out where my code is wrong.</p> <p>The basics of the code is that it's a 'address/balance' book. I've got a struct with my variables, but for some reason, my doubly linked list is getting all messed up and I can't figure out how. With a bit of fancy (not really) debugging, I've figured out that I think the fgets(string, 200, file); line is overwriting my head->name pointer somehow, which seems to throw the rest of the code off. Relevant code snippets here:</p> <p>Populating the List:</p> <pre><code>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; } </code></pre> <p>Then the add_node code:</p> <pre><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-&gt;name = name; addnode-&gt;streetaddress = streetaddress; addnode-&gt;city = city; addnode-&gt;state = state; addnode-&gt;zip = zip; addnode-&gt;balance = balance; if(head == NULL) { head = addnode; addnode-&gt;prev = NULL; } else { tail-&gt;next = addnode; addnode-&gt;prev = tail; } tail = addnode; addnode-&gt;next = NULL; } print_list(); return; } </code></pre> <p>The bug seems to be happening after the first time add_node is called and happens when fgets() goes off a second time. It's overwriting head->name with the entire fgets() line for some reason.</p> <p>There other random irritating bugs I haven't pinned down yet, but this one I think may be the source of the others.</p> <p>Full code is here, if it helps: <a href="http://pastebin.com/k0pqyvT0" rel="nofollow">http://pastebin.com/k0pqyvT0</a></p> <p>My guess is it's something to do with the head->name pointer being overwritten by fgets(), but I can't for the life of me figure out what exactly is doing this, any help/advice would be appreciated.</p> <p>Edit: Solution was to implement strdup() and duplicate the strings when they're passed to add_node(). Thanks for all the answers.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload