The Dink Network

sprintf is molesting

August 16th 2005, 11:53 PM
pillbug.gif
Drink
Peasant He/Him Chile
Don't drink 
Again with this thing.

sprintf(crap, "%d", spr[h].damage);
cr := &crap[0];

"crap" is type char[200],
"spr[h].damage" is int, and
"cr" is *char.
So, sprintf what does?
1) Moves the values directly, like if "damage" is FF00FF00, then "crap" will have [FF, 00, FF, 00].

2) Converts the value, like if "damage" is 000000FF (255), then "crap" will have [FF] (255).
It could be prudent if someone puts the description of that procedure, so I can stop molesting with this again.
August 17th 2005, 12:56 AM
custom_king.png
redink1
King He/Him United States bloop
A mother ducking wizard 
Neither.

If the value of damage is '114', then the value of crap is the array of characters '1', '1', '4', '\0'. It basically converts the integer value into a string.

I'm sure Merlin can come up with a better explanation.
August 17th 2005, 01:45 AM
wizardb.gif
merlin
Peasant He/Him
 
The purpose of sprintf is to write formatted output to a buffer. That buffer, in this case, is crap. The next argument to sprintf determines the type of the next argument...the most common are %s, string, and %d, integer. Since spr[h].damage is of type int, %d should be used.

The function essentially does exactly what printf does, except captures the STDIO output and puts it into the buffer. So, sprintf is converting the value of spr[h].damage to a string and storing it in crap. Redink's example is correct.
August 17th 2005, 01:15 PM
pillbug.gif
Drink
Peasant He/Him Chile
Don't drink 
Seems like it will not cause me more problem. Thanks.