newbie question on strings

Hi,

please can someone explain me why that:

sendmsg("teststring");

works ok with:

void sendmsg(char *str)
{ 
    while (*str)
    send(*str++) ;
}

but that:

String cw_message="asdfrgt";
sendmsg(cw_message);

void sendmsg(char *str)
{ 
    while (*str)
    send(*str++) ;
}

returns:

cannot convert 'String' tp 'char*' for argument '1' to void sendmsg(char*)'

?

thanks Panos, Athens

void sendmsg(char *str)

This defines the function as taking a pointer to a block of memory of type char (in other words, an array of characters).

String cw_message="asdfrgt";
sendmsg(cw_message);

This tries to call the function with an instance of a class that wraps an array of characters. Not the same thing at all.

cannot convert 'String' tp 'char*' for argument '1' to void sendmsg(char*)'

Which is exactly the same thing the compiler was telling you.

Thanks Paul,

and what is the proper way to fill a String as a variable and pass it to the specific (or any) function?

I read some posts but i can not imagine the solution.

and what is the proper way to fill a String as a variable and pass it to the specific (or any) function?

The first thing that has to happen is that the function needs to be defined to take a String as an argument, rather than a char array (or pointer to char).

void sendmsg(String str)
{
  // Deal with String
}

void loop()
{
   String msg = "SendMeToTheMoon";
   sendmsg(msg);
}

If the function is not (or can not be) written to take a String, you can extract the array from the string using the String::toCharArray() method.

Thank you very much Paul, that did the job:

String cw_message="TEST";
sendmsg(cw_message);

void sendmsg(String message)
{ 
    char str[200];
    int len=message.length();
    message.toCharArray(str, len+1); 
    for (int k=0; k<len; k++) {
    send(str[k]);
    }
void sendmsg(String message)
{ 
    char str[200];
    int len=message.length();
    message.toCharArray(str, len+1); 
    for (int k=0; k<len; k++) {
    send(str[k]);
    }

Some issues here. The toCharArray() method takes the size of the array to write to, not the amount of data to extract.

Second, there was no reason to extract the array. The String class also has a method to get the nth character (charAt()) that saves memory.

void sendmsg(String message)
{
  int len=message.length();
  for (int k=0; k<len; k++)
  {
     send(message.charAt(k));
  }
}

i didn't understand that:

The toCharArray() method takes the size of the array to write to, not the amount of data to extract.

but the send() works ok with different messages all day.

After all i can bet that your approach will be the best. i will try it and i will report on any problem.

Thanks agn and best regards.

but the send() works ok with different messages all day.

Until the length of the message exceeds the array that you have defined. If you pass the array size to getCharArray(), it will not overrun the array. If you pass the string length to toCharArray(), and that exceeds the size of the array, toCharArray will happily overrun the array. Bad things happen then.