SparkFun Forums 

Where electronics enthusiasts find answers.

For the discussion of Arduino related topics.
By mikehoover
#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]);
}
By Philba
#137575
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
Code: Select all
  
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
Code: Select all
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.
By mikehoover
#137604
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:
Code: Select all
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:
Code: Select all
void loop () {
  int = sizeof(strMessage) - 1;
  ...
}
Thanks!
By dmaunder
#147489
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.
By Philba
#147664
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.
By Joeisi
#147765
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.