I'm Having a spot of bother with string syntax

This is probably very simple to anyone with more Arduino experience than me (which is not very much) but I am having a slight problem gtting the correct syntax for passing a string to a function:

This extract of code shows the problem as it will not compile

This is my function

void sel_SOSD_TEXT_STR(int msize, int textlen, char text) // Send Text String to buffer
 Wire.write(msize); // message size
 Wire.write(0x06); // 
 Wire.write(0xcd); // Alpha
 Wire.write(0x08); // Register Bank
 Wire.write(0x00); // 
 Wire.write(0x07); // Register Offset
 Wire.write(0x00); // 
 Wire.write(textlen);  // Text String Length + 1 (zero terminator)
 Wire.write(text); // Text String

This is how I try to call the function

void setup()
  char text[] = "hello";
  int textlen = strlen(text);
  int msize = textlen + 0x07;
  sel_SOSD_TEXT_STR(msize, textlen, text);

This is the compile error: hdswitch_v0_02.ino: In function 'void setup()': hdswitch_v0_02:63: error: invalid conversion from 'char*' to 'char' hdswitch_v0_02:63: error: initializing argument 3 of 'void sel_SOSD_TEXT_STR(int, int, char)'

I tried some other combinations like

sel_SOSD_TEXT_STR(int msize, int textlen, char text); sel_SOSD_TEXT_STR(msize, textlen, char text[]); sel_SOSD_TEXT_STR(msize, textlen, text[]);

And probably a few others too

What is the correct syntax?

Cheers Rich

void sel_SOSD_TEXT_STR(int msize, int textlen, char* text)
void sel_SOSD_TEXT_STR(int msize, int textlen, char text)

change to

void sel_SOSD_TEXT_STR(int msize, int textlen, char *text)

You don’t actually send a copy of text into the function but its reference. That’s the rule in C.

Thanks for the swift replies, that sorted the problem and was quicker than me trying to work it out any further

For future reference are there also any other types of variable I need to pass to functions as a pointer? I assume arrays would be passed that way (as char is an array)

cheers Rich

as char is an array)

No, char is not an array.

Char can be an array as so can int, float and so on but not necessarily.

Any array or object needs to be a pointer in the function parameter.

dicky96: I assume arrays would be passed that way

Variables declared for arrays are pointers. That is, when you declare an array you are telling the compiler to reserve space for the array. In return, he will provide your with a pointer to the first element of the array. So when you do something like this:

int someArray[10];

The variable someArray is a pointer to the 0 index item.