Re: Newbie Coding: two bufs in sprintf

From: Chris Jacobson (fear@ATHENET.NET)
Date: 09/27/97


On 9/28/97 4:38 AM, Aaron Miller (Tarus50@concentric.net) stated:

>
>  sprintf(buf, "Text: %d", buf, GET_TEXT(ch));
>
>What is the second buf? It was not being used as a variable to print..

Actually what you probably saw was this:

sprintf(buf, "%stext %s", buf, whatever);

What this does is the equivalent of:

sprintf(buf2, "text %s", whatever);
strcat(buf, buf2);

Notice it is: "%stext %s", buf
So that "text %s" will be concatenated onto the end of what buf currently
contains, and the final string will be written back into buf.  Sprintf is
safe to do such overwriting of buffers becuase it uses secondary buffers
for safety reasons.

- Chris Jacobson


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     | http://democracy.queensu.ca/~fletcher/Circle/list-faq.html |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/08/00 PST