Strange C Problem

James C. Benz jcbst3 at cisunx.UUCP
Fri May 12 00:40:46 AEST 1989


In article <13812 at paris.ics.uci.edu> bvickers at bonnie.ics.uci.edu (Brett J. Vickers) writes:
>  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;
>}
>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().  

In case nobody has answered this yet, (unlikely, but I'm gonna do it anyway)

First, your line that prints the string has a \n in it.  It is followed by 
a printf that prints a single (special!) character WITHOUT a newline.  The
way your loop is set up, this second printf executes after EACH string from
the linked list is output, not after the whole list.  Further, since no
newline is output following the printf("&") the output buffer is not flushed
(stdout is only flushed on receipt of a newline.  Stderr is flushed on each
character)  So when the last printf("&") is executed, it waits in the stdout
buffer until the next call to printf("%s\n",.....), which is only when you
call output_msg again.  Try putting a printf("\n") OUTSIDE the loop before
exiting the function.  Or look up fflush in the manual.

Newsgroups: comp.lang.c
Subject: Re: mutual reference in structures
Summary: 
Expires: 
References: <6712 at medusa.cs.purdue.edu> <539 at lakart.UUCP>
Sender: 
Reply-To: jcbst3 at unix.cis.pittsburgh.edu (James C. Benz)
Followup-To: 
Distribution: 
Organization: Univ. of Pittsburgh, Comp & Info Sys
Keywords: 

 
-- 
Jim Benz 		     jcbst3 at unix.cis.pittsburgh.edu     If a modem 
University of Pittsburgh					 answers,
UCIR			     (412) 648-5930			 hang up!



More information about the Comp.lang.c mailing list