Pages: [1]   Go Down
Author Topic: How can concatenate value in a variable  (Read 521 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Sr. Member
****
Karma: 0
Posts: 368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,
I am a beginer with Sketch.

I would like to know how can I concatenate value.

For exemple
Code:
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:
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
Logged

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

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
char a = "h";

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

Global Moderator
Netherlands
Offline Offline
Shannon Member
*****
Karma: 211
Posts: 13478
In theory there is no difference between theory and practice, however in practice there are many...
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

google for the strcat() command
Logged

Rob Tillaart

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

Offline Offline
Sr. Member
****
Karma: 9
Posts: 254
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Worst state in America
Offline Offline
God Member
*****
Karma: 32
Posts: 792
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
   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.
Logged

Gentlemen may prefer Blondes, but Real Men prefer Redheads!

Offline Offline
Sr. Member
****
Karma: 0
Posts: 368
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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!!
Logged

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

Offline Offline
Edison Member
*
Karma: 116
Posts: 2205
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Pages: [1]   Go Up
Jump to: