Loading...
Pages: 1 [2]   Go Down
Author Topic: Why does this function crash a MEGA 2560?  (Read 289 times)
0 Members and 1 Guest are viewing this topic.
Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 311
Posts: 35470
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
sprintf returns an integer, not a char array. I can't do something like this:
No, you can't. But, you can (and should) do:
Code:
char buffer[20];
sprintf(buffer, "L%05d",altitude);
ea.drawText(100,100, buffer);

Quote
As I wrote, I have to display a rather big amount of various numerical data, so I'm looking at some major rewriting... *sigh*
Quit moaning and get started.  smiley-cool

The fact that you started out in the wrong direction doesn't help you.

You may be able to write some functions to minimize the amount of work involved.
Logged

Global Moderator
Melbourne, Australia
Offline Offline
Shannon Member
*****
Karma: 218
Posts: 13896
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Try what I said in reply #11. That might fix the crashes.

In any case it shouldn't be hard to rewrite your function to work. Here's an example:

Code:
const char * leadingZero (const int data, const int len)
  {
  static char buf [11];
  sprintf (buf, "%010d", data);
  return &buf [strlen (buf) - len];
  }  // end of leadingZero

void setup ()
  {
  Serial.begin (115200);
  Serial.println (leadingZero (42, 3));
  Serial.println (leadingZero (42, 4));
  Serial.println (leadingZero (42, 5));
  Serial.println (leadingZero (42, 6));
  }  // end of setup

void loop () {}  

Test output:

Code:
042
0042
00042
000042
« Last Edit: February 10, 2013, 05:34:36 pm by Nick Gammon » Logged


Finland
Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Milton Keynes UK
Offline Offline
Tesla Member
***
Karma: 88
Posts: 6286
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Finland
Offline Offline
Newbie
*
Karma: 0
Posts: 29
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I tried it basically like in that example, just without the 'const' and the 'static'. With ints and chars that works...
Logged

Global Moderator
Melbourne, Australia
Offline Offline
Shannon Member
*****
Karma: 218
Posts: 13896
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Don't get rid of "static". I put it there for a reason. Without it you are returning the address of a variable on the stack, which no longer exists.
Logged


Pages: 1 [2]   Go Up
Print
 
Jump to: