cancel
Showing results for 
Search instead for 
Did you mean: 

Is there a way to prevent sprintf from writing a terminating nul past the end of the destination

JBonn
Associate III
 
11 REPLIES 11
Piranha
Chief II

Not only terminating null character, but it can write anything past the end. It's strongly recommended to not use it at all. Instead snprintf() must be used.

No,

But sprintf() does return the length of the string added, but whatever buffering you have needs to be able to accommodate the C string properly.

The length would allow you to advance the pointer to the NUL, or memcpy() to a spot you can't tolerate the NUL

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Tesla,
Thanks for your answer.
Please help me get around this problem.
Here is my current code.
char char One_LCD[1], Two_LCD[], Three_LCD[];
uint8_t Gain; // Gain is always a single digit value
sprintf (One_LCD,"%1d",Gain); // I think you told me that this pollutes Two_LCD
OLED_countDraw (1,1,66,One_LCD);
OLED_countDraw is a program I wrote for STM32L4 chips to draw characters on an OLED graphic display
The above code will draw a 6 by 8 character on line 1 of 4 lines starting at the 66th place on 128 place line.
It is my way of placing characters at specified places on a graphic display.
How do I keep the sprint code above from polluting the Two_LCD space?
Would the following code display the correct Gain and not pollute Three_LCD?
sprintf (Two_LCD,"%1d",Gain);
OLED_countDraw (1, 1, 66, Two_LCD);
Jerry Bonner

Why can't you make these strings wide enough to carry the data?

You don't show the prototype for OLED_countDraw, if it takes a char, pass a char

char char String[4];

sprintf (String,"%1d",Gain);

OLED_countDraw (1,1,66,String[0]); // First character

OLED_countDraw (1,1,66,String[1]); // Second character

If it expects the string, ie char *

OLED_countDraw (1,1,66,String); // Pass as string

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

If Gain is always a value between 0 and 9, wouldn't something more simple like this work?

char g = '0' + (char)Gain;

char g = (char)(0x30 + Gain);

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Piranha<>, WOW! Thanks! Did not know snprintf existed. Also did not know that printf was so difficult to deal with. Jerry Bonner

There is no need for those casts. In the first line the addition will anyway be done on "int" type because of how integer promotion is specified. In the second line the cast is totally useless, because assignment does implicit casting anyway.

Do you constantly need to be such an *** ?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
JBonn
Associate III

Tesla, I do not understand your reaction. I said "WOW" because i liked your response and followed that with a "Thanks". I apologize to you if if you think I responded to your answer in a negative way. I meant my response to be a "Thank You".