Pages: [1]   Go Down
Author Topic: Strange atol results with char array  (Read 2817 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 375
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I'm getting some strange results when using atol depending on when I declare an array.

Code that fails:
Code:
long testVal1;
long testVal2;

void setup()
{
  Serial.begin(9600);
  Serial.println("Ready");
  
  char testChar1[] = {'5', '0'};
  char testChar2[] = {'9', '9'};
  testVal1 = atol(testChar1);
  testVal2 = atol(testChar2);

  Serial.println(testVal1);
  Serial.println(testVal2);

}

void loop()
{

}

Results:
Code:
Ready
5099
99

Code that works:
Code:
long testVal1;
long testVal2;

void setup()
{
  Serial.begin(9600);
  Serial.println("Ready");
  
  char testChar1[] = {'5', '0'};
  testVal1 = atol(testChar1);
  char testChar2[] = {'9', '9'};
  testVal2 = atol(testChar2);

  Serial.println(testVal1);
  Serial.println(testVal2);

}

void loop()
{

}

Results:
Code:
Ready
50
99

Am I missing something?
Logged

Copenhagen, Denmark
Offline Offline
Edison Member
*
Karma: 33
Posts: 1228
Have you testrun your INO file today?
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Terminating nul byte of a string ? The atol does not get to know the length of the array.
Code:
char testChar1[] = {'5', '0', 0x00};
  char testChar2[] = {'9', '9', 0x00};
Logged

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 375
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Interesting, that does work.  Thank you.  From looking at the array reference page I thought that the compiler added that when the array was declared.

http://arduino.cc/en/Reference/Array

It also works if I use:
Code:
char testChar1[3] = {'5', '0'};
char testChar2[3] = {'9', '9'};
« Last Edit: September 14, 2010, 06:01:44 pm by Digger450 » Logged

0
Offline Offline
Faraday Member
**
Karma: 16
Posts: 2855
ruggedcircuits.com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

You are getting lucky. It just so happens that there are 0 bytes lying around in memory that terminate your strings. The compiler DOES automatically add a 0 at the end if you do this:

Code:
char testchar1[] = "50";

But not if you declare it your way.

--
The Gadget Shield: accelerometer, RGB LED, IR transmit/receive, light sensor, potentiometers, pushbuttons
Logged

0
Offline Offline
Sr. Member
****
Karma: 0
Posts: 375
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Good to know.

Any reason why the compiler should not add the null terminator?  Basically, I'm wondering if this is a bug or a feature.
Logged

0
Offline Offline
Faraday Member
**
Karma: 16
Posts: 2855
ruggedcircuits.com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The C compiler sees a "char" as really just a small integer. So:

Code:
char stuff[] = {'0', '1'};

simply says that you want an array of small integers, and you want two of them. It doesn't know you're going to eventually interpret those integers as textual characters.

Now this:

Code:
char stuff[] = "01";

says you want an array of small integers but you want three of them, since initializing with a string says you want a string!

--
The Gadget Shield: accelerometer, RGB LED, IR transmit/receive, light sensor, potentiometers, pushbuttons
Logged

Pages: [1]   Go Up
Jump to: