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???
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.
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:
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.
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.