Passing arrays to functions, unexpected results

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]);

}

This is tied up in the way C works. Arguments to functions are passed by value. In this case you are passing the address of the array to the function. In the function, the argument is seen as a pointer to a character. (there is no string data type.) This is exactly equivalent

int getLength(char *theString) { //PASSING BY REFERENCE OR VALUE???
  return sizeof(theString)/sizeof(theString[0]);
}

That’s why you get 2 (the size of a pointer in AVR GCC). Try this

char strMessage[] = "Hello World";

void setup() {
  Serial.begin(19200);
}

void loop () {
  int length = getLength(strMessage); 
  Serial.print(sizeof(strMessage));
  Serial.print(" ");
  Serial.print(sizeof(strMessage[0]));
  Serial.print(" ");
  Serial.println(length);
  delay(1000);
}

int getLength(char theString[]) { //PASSING BY REFERENCE OR VALUE???
  return sizeof(theString)/sizeof(theString[0]);
}

exercise left to the student - why is sizeof(“Hello World”) 12 and not 11?

If you want the length of a string (er character array), you should count the characters. If you can answer the above question then you will know how to do it.

Phil

ps, please use the code tags when posting code. makes it look correct and is easier to copy.

If you are sure you have a null-terminated string, you can use strlen() to find it’s length.

/mike

I think the 12th “mystery” char would be the null value to indicate the end of the string. So we have to take that into account when finding the length of a string.

My solution is to count until I get the null character:

int getLength(char *theString) { //PASSING ARGUMENT BY VALUE
  int strLength = 0;
  while (theString[strLength] != NULL){
    strLength++;
  }
  
  return strLength;
}

Using the function getLength() is a matter of exercise when I can just do:

void loop () {
  int = sizeof(strMessage) - 1;
  ...
}

Thanks!

I am also pretty new to Arduino and C for that matter. I am struggling with passing a string or char array to a function.

I have the following

char cool_on_25 = “1110001001101001101100100100000000000000000000100000110001001000001101100101000100011000100000000000000000001000000000000000010000000000010101110”;

and am then calling the function as follows

irsend.sendMITSUBISHI(cool_on_25, 145);

I have tried every combination of char/string etc in the definition

IRsend() {}

void sendMITSUBISHI(char *bitmap, int nbits);

but none of them work, what am I doing wrong here?

I don’t need to modify the string, so only need to pass by value.

well if sendMitsubishi is similar tothe other send routines, the first argument is an unsigned long, not a pointer to an array. I suggest an understanding of what you are calling is in order.

It isn’t the same. The Mitsubishi aircon remote sends a bitmap of 292 bits, so I needed to add a new function that takes a parameter of type char array.

and you don’t want to show your modified function because…? For more information, check here- viewtopic.php?f=32&t=33083

Well, you have created an ascii string. I bet your bitmap is expecting 0x1 and 0x0.

Could it also be packed? that is, using your data, you want 0x11100010, 0x01101001…

You would have to directly specify it is a group of bits, instead of the char string it is looking for. This is usually done by adding the string “0b” (without quotes) to the beginning of the declaration. So for example, the binary constant for 0xD4 is 0b1101010.

This may give an error because 292 bits will not evenly fill up a char string. You have a character with 4 bits unused.