- Thu Dec 29, 2011 10:04 am
#137565
I am a complete novice. Here is an example of my code. I have a function that will return the length of a char array, but it does not function properly. I would expect the length to be 11, not 2. Perhaps my getLength() method is passing be reference and not passing the actual char array, so the sizeof()/sizeof([0]) delivers a length of 2 - I still don't know why. Comments/suggestions? Thanks!
Here is an example arduino code:
char strMessage[] = "Hello World";
void setup() {
//some stuff...
}
void loop () {
int length = getLength(strMessage);
for (int i = 0; i < length; i++){ //ONLY ITERATES TWICE, EXPECTED ELEVEN TIMES
//iterate thru array...
}
}
int getLength(char theString[]) { //PASSING BY REFERENCE OR VALUE???
return sizeof(theString)/sizeof(theString[0]);
}
Here is an example arduino code:
char strMessage[] = "Hello World";
void setup() {
//some stuff...
}
void loop () {
int length = getLength(strMessage);
for (int i = 0; i < length; i++){ //ONLY ITERATES TWICE, EXPECTED ELEVEN TIMES
//iterate thru array...
}
}
int getLength(char theString[]) { //PASSING BY REFERENCE OR VALUE???
return sizeof(theString)/sizeof(theString[0]);
}