Is there a way to prevent sprintf from writing a terminating nul past the end of the destination
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-09-06 12:02 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-09-06 02:40 PM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-09-06 03:16 PM
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
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-09-07 07:40 AM
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-09-07 08:47 AM
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
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-09-07 08:58 AM
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);
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-09-07 09:39 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-09-07 10:42 AM
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-09-07 10:45 AM
Do you constantly need to be such an *** ?
Up vote any posts that you find helpful, it shows what's working..
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-09-07 12:07 PM
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".