Passing a char variable to a function

I have a function that receives a char and displays it on an LCD screen. The start of the function is :

void LCD_2(char LCDdata[],int Spos){

In the Loop, I call it with :

LCD_2("Home", 0);

The above solution is working, but I want to replace the "Home" (hard coded text) with a variable.

I have tried, in the pre-setup :

char LCD2ah[];

and in the loop code :

LCD2ah[] = "Home";

but it is not compiling. Where am I going wrong ?

but it is not compiling.

What errors are you getting?

char LCD2ah[];

What size array does this create?

ok. if I change the variable declaration to :

char LCD2ah[5];

then the error is: expected primary-expression before ']' token. I really don't understand what this means.

Post the whole sketch.

LCD2ah[] = "Home";

The compiler already knows that LCD2ah is an array. You don't need to tell it that again.

This is NOT how you assign data to elements of an array. For strings (NULL terminated arrays of chars) there are useful functions for assigning strings to arrays:

LCD2ah[0] = '\0';
strcat(LCD2ah, "Home");

Many Thanks PaulS

The following works :

char LCD2ah[5];  // will never hold more than 4 characters of text

strcat(LCD2ah, "Home");

LCD_2(LCD2ah, 0);

void LCD_2(char LCDdata[],int Spos){
strcat(LCD2ah, "Home");

To avoid possible future funnies, you may wish to consider using "strcpy" instead.
If "LCD2ah" already had a value, or was an automatic variable, "strcat" could give unwanted array overflow, unless you pre-terminate it, as PaulS showed you.

Thanks AWOL

So if I was to use strcat, I first need to add :

LCD2ah[0] = '\0';

to clear the array before re=populating it ?

Correct - the "cat" in "strcat" is short for "conCATentate", so the function first looks for a null terminator in the destination string, and overwrites it, and subsequent bytes with the source string until the terminator in the source string is encountered.
If your destination string has never held a string, or is already filled with a string, "strcat" could go stomping over memory it shouldn't ought to, with the usual hilarious consequences.

The official Arduino reference states:

"All of the following are valid declarations for strings.

char Str1[15];
char Str2[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o'};
char Str3[8] = {'a', 'r', 'd', 'u', 'i', 'n', 'o', '\0'};
char Str4[ ] = "arduino";
char Str5[8] = "arduino";
char Str6[15] = "arduino";
"

When PaulS states,

This is NOT how you assign data to elements of an array.

it makes me curious as to what the practical difference (pros and cons) is between using either strcat or strcpy and the way recommended by the reference pages of the Arduino Website.

@lemming:

Do you see any of those forms here:
LCD2ah[] = "Home"; ?

Although it is missing the "char" typing, I thought

LCD2ah[] = "Home";

was essentially the same form as

char Str4[ ] = "arduino";

Both use direct assignment rather than strcat or strcpy.

Sorry, posting from my phone, but I'd be surprised if the first even compiles.

Both use direct assignment rather than strcat or strcpy.

When the variable is declared and initialized in one statement, the examples shown are possible.

When the variable is declared, and later needs to be initialized/reset, it's a whole different story.

Sorry, didn't notice that he had defined it prior to setup as a global.