Go Down

Topic: How can concatenate value in a variable (Read 665 times) previous topic - next topic

pierrot10

Hello,
I am a beginer with Sketch.

I would like to know how can I concatenate value.

For exemple
Code: [Select]
char a = "h";
char b = "i";

char c = ""

At the end c should print
Serial.print("hi").


In php we can do like this (with the dot begore the ega)
Code: [Select]
a= "h";
b= "i";
c = a
c .= b

echo c  print  "hi"


With Sketch, I tried ".=" but it does not work
I tried "+=" but it ford not work as well.

Is there something like .= with PHP?

Cheers and thank
Il ne suffit pas de tout savoir, la persévérance, c'est déjà presque tout!

dhenry

Code: [Select]
char a = "h";

Read up on a good C book and understand how "h" differs from 'h'.

robtillaart

Kerningham and Ritchie, C 2nd edition is a very good book.

google for the strcat() command
Rob Tillaart

Nederlandse sectie - http://arduino.cc/forum/index.php/board,77.0.html -
(Please do not PM for private consultancy)

tobyb121

The way c handles strings is a bit confusing for beginners. Effectively a string is stored in an array of characters, so the string "hello" is stored as ['h','e','l','l','o',0] (note that single quotes: ' are used to tell the compiler that they are a single characters, and double quotes " are for strings, whch the compiler "converts" into characters for you). When you do x="hello", x is actually the pointer to the first element in the array in the memory. The 0 at the end of the array (it's not the character '0') is called a null byte and tells the program that the string stops there.
When you look at a function like Serial.print, it takes a char* that points to the first character, and then steps through the array processing each character until it reaches a 0 (null byte).

What this means is that if you are trying to join the two strings, "hello" and "there" you need to join the two arrays: ['h','e','l','l','o',0] and ['t','h','e','r','e',0]. There are functions in the library string.h which make it easy to do these kinds of things (look at strcat http://www.cplusplus.com/reference/cstring/) but these libraries can be quite "heavy" (take up alot of the available program memory) so for microcontrollers, some programmers write these kind of simple functions themselves, to save the space of including the whole library.

Krupski


Hello,
I am a beginer with Sketch.

I would like to know how can I concatenate value.

Is there something like .= with PHP?

Cheers and thank


First of all, you need single quotes... i.e. char a = 'h';

Next, how about doing this:

Code: [Select]

   char buffer[3];
   buffer[0] = 'h';
   buffer[1] = 'i';
   buffer[2] = 0; // null terminate string
   Serial.print(buffer);


Note that the buffer has to be declared as long as your string, plus one more for the null terminator. That is, to do "Hello" you need buffer[6]; - 5 for the hello and 1 for the null at the end.

Don't know WHY you would want to do it in such a convoluted way, but it will work.
Gentlemen may prefer Blondes, but Real Men prefer Redheads!

pierrot10

Dera All and specialy tobyb,
Thank fro your explication. By the way, I discover what tobyb explain to me.
I also checked strcat()
Thank a lot!!
Il ne suffit pas de tout savoir, la persévérance, c'est déjà presque tout!

dhenry

Quote
The way c handles strings is a bit confusing for beginners.


Yes. I have quite a few "experienced" beginners who got confused by that as well.

The string.h has a bunch of functions you can use for strings, strcat() being one of them.

Go Up