Having trouble concatenating a char*

Hi,
So I am trying to print some array values out to my LCD:

  string = "";
  string = Chord[navigator];
strcat(string, " : ");
strcat(string, Camelot[navigator]);
 u8g.firstPage(); 
 do {
draw(string);
 } while( u8g.nextPage() );

This is being done on a button interupt. It works to an extent, each time the button is pressed it updates the lcd, however it seems to concatenate forever as far as I can tell based on whats coming out on the lcd.
The first time I would get what I expect, but the second time I get another ':' and the value stored in the next Camelot[navigator]
I thought that

string = "";

was going to clear the string for the next update of the lcd, however it doesnt by the looks of things??

a.mlw.walker:
Hi,
So I am trying to print some array values out to my LCD:

  string = "";

string = Chord[navigator];
strcat(string, " : ");
strcat(string, Camelot[navigator]);
u8g.firstPage();
do {
draw(string);
} while( u8g.nextPage() );



This is being done on a button interupt. It works to an extent, each time the button is pressed it updates the lcd, however it seems to concatenate forever as far as I can tell based on whats coming out on the lcd.
The first time I would get what I expect, but the second time I get another ':' and the value stored in the next Camelot[navigator]
I thought that


string = "";



was going to clear the string for the next update of the lcd, however it doesnt by the looks of things??

What is "string"? If it a char* as you suggest in the subject, then use memset() or set the first byte of the pointer to "\0"

Have you used Serial.print to check that string is what you think it is? Do you set the cursor between writes (lcd.home() or something)? It may be that concatenation happens because you don't return the cursor and/or clear the lcd between writes.

Anyway, the way use the "string" variable seems suspicious:

string = "";                            
string = Chord[navigator];   
strcat(string, " : ");
strcat(string, Camelot[navigator]);

If string is of type "char *", you need to allocate memory for it.

This is unnecessary

string = "";

because the after the next line string holds the value of Chord[navigator].

string = Chord[navigator];

If Chord[navigator] holds a pointer to a string, has it enough memory allocated for " : " and the value of Camelot[navigator]?

strcat(string, " : ");
strcat(string, Camelot[navigator]);

The code may seem to work even if you haven't allocated enough memory for string. if it overflows (strcat writes beyond the allocated memory), depending on what variables etc. the memory holds it may or may not crash. But you really should make sure you have allocated enough memory, e.g.,

char string[20]; // allocate enough memory for Chord[navigator], " : " and Camelot[navigator]
strcpy(string, Chord[navigator]);   
strcat(string, " : ");
strcat(string, Camelot[navigator]);

Yeah, string is char*:

char* string;
string = Chord[navigator];
strcat(string, " : ");
strcat(string, Camelot[navigator]);
 u8g.firstPage(); 
 do {
draw(string);
 } while( u8g.nextPage() );

draw is the function to draw to the lcd - its a graphical lcd, so it redraws the entire screen.
However if I try to allocate memory to it like char* string[20] or char string[20], I get the error:
incompatible types in assignment of 'char*' to 'char [20]

char string[20]; // makes a character array named string with 20 elements

char *string; // makes a char pointer variable named string

char *string[20]; // makes a char pointer array named string with 20 elements, all char pointers

So what happens when you make a char pointer named string and then you try and make a char array named string?

Better question is what happens when you don't bother learning how a language works before coding a project?

However if I try to allocate memory to it like char* string[20] or char string[20], I get the error:
incompatible types in assignment of 'char*' to 'char [20]

On what line do you get the error "incompatible types in assignment of 'char*' to 'char [20]"? I don't know what the signature of draw function in your code is, but I'm pretty sure the piece of code I posted works as I explained.

Did you declare "string" both as "char *string;" and "char string[20];"? You should only keep the latter. And 20 here is only an example. Make sure that the string is long enough to hold the whole text you want to display.

yeah, draw is expecting a char*
i.e

void draw(char* line){...}

So what happens when you make a char pointer named string and then you try and make a char array named string?

No I haven't declared two variables called string of different types, the code is as is in my last post however I replaced the line char* string with the variants I posted after.
The code worked fine before I wanted to concatenate strings. At that point it didnt seem to clear each time it was called.
First time I would see the string

Chord[navigator] : Camelot[navigator]

next time would be:

Chord[navigator] : Camelot[navigator] : Camelot[navigator]

and by the third time my lcd is too small so I would just be able to see probably:

: Camelot[navigator] : Camelot[navigator] : Camelot[navigator]

Did you try the piece of code I suggested? The way you manipulate the string in the code you posted is wrong. It may seem to work at some extent, but will surely mess up the memory.

This is still wrong

char* string;
string = Chord[navigator];
strcat(string, " : ");
strcat(string, Camelot[navigator]);
 u8g.firstPage(); 
 do {
draw(string);
 } while( u8g.nextPage() );

You are still not allocating memory for string. When you do the assignment:

string = Chord[navigator];

you start using the memory allocated for Chord[navigator]. Lets assume that the initial value of Chord[navigator] is "foo" and Camelot[navigator] is "bar".
After your code executes the strcat statements, both string and Chord[navigator] point to char array "foo : bar". (And probably writes beyond the memory you allocated for Chord[navigator]).
When your code runs the strcat for second time (e.g. in the loop fuction) you are concatenating "foo : bar", ": " and "bar". And after third iteration string will be "foo: bar : bar : bar" and so on. Seems to be pretty much what you said you saw on the lcd

With each iteration the string keeps growing untill it runs beyond the Arduino's address space. At that point, if not earlier, you sketch will crash.

A (char *) points to a char variable. Usually the char variable is in an array of char variables, possibly a many-dimension array. Without a char variable to point to, a (char *) isn't much use.

You're supposed to set up the spaces and pointers for the functions to use.

Ahh sorry guys I miss understood that I needed to strcat the first one as well. Strings have always given me a little bit of confuson.
Thanks for all your help on this.

EDIT:
No sorry that still continues to ammend the string (the string is globally defined) I need to clear it
I tried:

 strcat(string,Chord[navigator]);
strcat(string, " : ");
strcat(string, Camelot[navigator]);
string[19] = '\0';

Because I read \0 would end the string, but that doesnt work either?

Given the following declaration:

char myString[50];

To set everything to null (clear the string):

memset(myString, '\0', 50);

To copy data into the beginning of the string:

strcpy(myString, someOtherString);

This assumes someOtherString is also a null-terminated string.

Ahh got it. I can basically just set the FIRST value to '\0'
Thanks

a.mlw.walker:
Ahh got it. I can basically just set the FIRST value to '\0'
Thanks

And then use strcat?

You may as well just use strcpy

string[19] = '\0';

You don't need to do that. strcat adds the trailing zero automatically.

Ahh got it. I can basically just set the FIRST value to '\0'

You can do that, or you can use strcpy instead of strcat to copy the first value into string as I suggested:

strcpy(string, Chord[navigator]);

If you do that, there is no need to separately "clear" the string by setting the first char to '\0'.