Go Down

Topic: array initialization discrepancy? (Read 1 time) previous topic - next topic

jamie

Feb 13, 2007, 04:22 am Last Edit: Feb 13, 2007, 04:25 am by jamie Reason: 1
hi all!

i've been trying to initialize a an array of chars, and i've noticed something weird.  it seems as if i'm getting an extra character when i initialize the array one way versus another.  

i.e.: when i initialize

Code: [Select]
char serTest[2]={'O','K'};

it's equivalent to this (it's adding the carriage return)

Code: [Select]
serTest[0]='O';
serTest[1]='K';
serTest[2]=13;


when i call  
Code: [Select]
Serial.print(serTest);
just me?  on purpose?  what?

lub,
j

kg4wsv

Calling Serial.print with a type of char * expects a null terminated string.  Your array is not a normal null terminated string, so the function is traipsing off into memory printing stuff beyond the end of your array until it finds a null.

If you are using it as a string, your array should be initialized as

Code: [Select]
char serTest[3] = { 'o', 'k', '\0'};

or

Code: [Select]
char serTest[] = "ok";

These are identical initializations.

-j

Go Up