I have written this function in a project. If I call it with small numbers and len = 3, it works. If I call it with small numbers and len = 5, the MEGA freezes...
Any ideas, anyone?
String leadingZero(String data, int len) {
String result="";
int i;
if (data.length() < len) {
for (i=0;i<(len-data.length());i++) {
result += "0";
}
}
result+=data;
return result;
}
The 'result' String is local to the leadingZero function which means it is allocated on the stack when you enter the function and it is freed when you leave. So you are returning a pointer to a non-existent String.
It's best not to use Strings at all because even a MEGA doesn't have a lot of spare RAM to allow much String manipulation.
I don't know how to use char arrays for my stuff. I wrote a library for controlling intelligent TFT displays and my functions like drawText use Strings because otherwise just everything blows up in my face, because I have to display a ton of numerical data and I have no idea how to convert e.g. an integer into an array of char.
Then you need to learn. Converting altitude to a String so you can prepend leading 0s is silly, when the sprintf() function can do it for you. In fact, it can produce the whole string to draw without the need concatenate stuff in the function call.
Of course, exactly how to use sprintf() depends on the data types involved. You can either post all of your code, research the issue yourself, or head over to http://snippets-r-us.com for help. Your choice.
Is there any function that return a char array from in integer like char* function(int integer) or is there only functions which I have to give a char array as parameter to fill?
Where would I find the right documentation for sprintf? Google returns uncountable amounts of hits?
Is there any function that return a char array from in integer like char* function(int integer)
What does thins mean? Are you asking if there is a function that will contain a string that represents the integer value? The itoa() function does that.
Where would I find the right documentation for sprintf? Google returns uncountable amounts of hits?
No, it doesn't. It actually counts them for you. Just pick one. Really, it won't try to sell you a sprintf.
Is there any function that return a char array from in integer like char* function(int integer)
What does thins mean? Are you asking if there is a function that will contain a string that represents the integer value? The itoa() function does that.
I don't know how to explain better. I'm no English native speaker. sprintf returns an integer, not a char array. I can't do something like this:
ea.drawText(100,100,'L',sprintf("%05d",altitude))
As far as I understand it, I have to declare some buffer variable first (which's size I need to know in advance), use sprintf to put the formatted string into the buffer and then use the buffer in drawText(). That's feels kinda cumbersome to me.
Yes you do, but you seem to have a pretty good idea of how large a buffer you need, as you are requesting a 5 digit number. You need to allow for the trailing string terminator, so a buffer of 6 bytes would be a good approximation, assuming you are sure the number will never be larger than 5 digits.
Unfortunately, that wasn't the only occurrence... As I wrote, I have to display a rather big amount of various numerical data, so I'm looking at some major rewriting... sigh
May I ask, why the 'const' as the type and in the parameters. I'm asking because I tried to write a function that returns a char array before and it didn't work.
The const just means you promise not to change it (the value is read-only).
I don't know how you tried to return the char array, but if it was a local automatic variable then it will have been allocated on the stack and the content becomes undefined as soon as the function returns.