so have program put head first c. chapter 6 - data structures chapter... problem output displays previous listed entries last entered name standard input. instead showing printed once program prints twice. hard me describe. if copy , paste text-editor on machine , run code see mean.
the book shows program taking file of island names using < redirection tool. when try prints first name second name , first name. next name , second , first name...then next name , third, second, , first name...etc depending how many names there are. behavior occurs when entering text in terminal in standard input.
if change code display(next) works closer expect still prints out blank line , there memory leaks
this code pretty on head can figure out why printing this?
i ask @ head first c discussion board wanted ask stackoverflow first , immediate answer.
my code below. if copy , paste text editor should not wall of text.
happy coding.
#include <stdio.h> // basic input output #include <stdlib.h> // obtaining , releasing heap memory malloc , free... #include <string.h> // stringdup method typedef struct island { char *name; char *opens; char *closes; struct island *next; } island; void display(island *madonna); island* create(char *name); void release(island *start); int main() { /* create islands */ island *start = null; island *i = null; island *next = null; char name[80]; puts("enter island name..."); for(; fgets(name, 80, stdin) != null; = next) { next = create(name); if(start == null) start = next; if (i != null) -> next = next; display(start); } release(start); } // display method void display(island *start) { island *i = start; if (i == null) puts("i equals null "); for(;i != null; = ->next) { printf("name: %s open: %s-%s\n", i->name, i->opens, i->closes); } } // create method island* create(char *name) { island *i = malloc(sizeof(island)); i->name = strdup(name); i->opens = "09:00"; i->closes = "17:00"; i->next = null; return i; } // release method void release(island *start) { island *i = start; island *next = null; for(; != null; = next) { next = i-> next; free(i->name); // must free first because of strdup uses heap memory free(i); } }
the code working-as-designed (wad). designed print complete list after each entry read — that's display(start)
in loop. either echoing input (printf("read: %s", name);
there's no newline in format because name still includes newline) or tagging display printf("printing list:\n");
before call display()
(or both). if rid of newline name, you'll need adjust 'echo' operation.
learning how create helpful diagnostic messages valuable technique; 1 of key points ensure output lines end newline there's decent chance you'll see printing occurs, rather indeterminate time later. key point printing inputs know code working on rather thinking know code working on. printing complete list on each iteration helps ensure list being constructed correctly. can find examples on list not constructed correctly (e.g. first address of struct). had complete list been printed on each iteration, problem have been more obvious.
the trouble, therefore, seems expectations not match code designed do.
Comments
Post a Comment