Arduino Forum

Forum 2005-2010 (read only) => Software => Bugs & Suggestions => Topic started by: jamie on Feb 13, 2007, 04:22 am

Title: array initialization discrepancy?
Post by: jamie on Feb 13, 2007, 04:22 am
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]

when i call  
Code: [Select]
just me?  on purpose?  what?

Title: Re: array initialization discrepancy?
Post by: kg4wsv on Feb 13, 2007, 01:14 pm
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'};


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

These are identical initializations.