Path: utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!purdue!decwrl!pyramid!ctnews!mitisft!kemnitz From: kemnitz@mitisft.Convergent.COM (Gregory Kemnitz) Newsgroups: comp.lang.c Subject: Re: Strange C Problem Summary: printf needs a newline to print this... Keywords: Linked Lists Message-ID: <688@mitisft.Convergent.COM> Date: 10 May 89 20:11:19 GMT References: <13812@paris.ics.uci.edu> Distribution: na Organization: Convergent Technologies, San Jose, CA Lines: 49 In article <13812@paris.ics.uci.edu>, bvickers@bonnie.ics.uci.edu (Brett J. Vickers) writes: . . I've come across a very strange problem in one of my programs. I'll . list the function that's causing the problems and then discuss what . those problems are. . . Globals: struct msg_node *first_msg, *last_msg; . . void output_msgs() . { . struct msg_node { . char string[160]; . struct msg_node *next; . struct msg_node *prev; . } *current, *temp; . . current = first_msg; . while (current != NULL) { . printf("%s\n",current->string); . printf("&"); . temp = current; . current = current -> next; . free (temp); . } . first_msg = NULL; . last_msg = NULL; . } . . Now, this function is supposed to output all the messages that are on . the linked list from first to last. But something strange happens. . After the function has output its last message, it fails to continue . on to the next line (printf("&")). The ampersand isn't output until ^^^^^^^^^^^ . the next call to output_msgs(). . . . Why is this happening? All help appreciated. . No mystery here... printf needs a newline to print its string. the &'s are being buffered by this routine until the printf with a newline is called. A fix would be to rework the printf's in the loop or to put a printf("\n") at the end of the routine. This routine may also have a bug in that its first call will not have the ampersand before the string, but all calls after it will. Greg Kemnitz