I'm working on a project that I need to do some formatting to a string, I was having some weird bugs so I decided to start isolating parts of my project to find out what might be causing the issue.
Part of my project uses the sprintf function to format a char array for me. I know the arduino does not support floating points numbers by default with sprintf, currently I am just working with integers and char[] right now.
The issue I have found is that the serial output as soon as I start using sprintf goes all crazy! It'll work fine before, but as soon as I even call on the function even if I am not printing the data the serial output goes crazy!
My only thought was that maybe the serial library uses sprintf? I've posted the code that shows what happens.
Writing to a zero length string is not the cleverest thing you could do.
(Or should that be " getting sprintf to write to a zero length string "? Whatever)
AWOL:
Writing to a zero length string is not the cleverest thing you could do.
(Or should that be " getting sprintf to write to a zero length string "? Whatever)
What would be the correct way? Create a string with a set length that I know is within the range of my final string?
Don't use more than 63 because that is the default size of the tx buffer in the serial library. You can make it smaller to your needs (remember space for thr terminating nul character as said above). You also might want to consider snprintf so you will never get an overflow.
Awesome, thanks for you all your help I got everything working now. Now I know not to initialize a zero length char[]...I suppose I would need to call malloc() if I were to want to change a zero length char[] no?
For this project I won't need to go over 63 characters, but its good to know that I can use snprintf if I need to go larger.
isaucy:
For this project I won't need to go over 63 characters, but its good to know that I can use snprintf if I need to go larger.
That will not work; snprintf will just throw everything away that exceeds the the size of the character array (minus one).
isaucy:
Awesome, thanks for you all your help I got everything working now. Now I know not to initialize a zero length char[]...I suppose I would need to call malloc() if I were to want to change a zero length char[] no?
No, you will just declare a pointer and use that in combination with malloc.