How to combine array?

Hi,
May I use:

int dataNumber = 0; 
char msgA[asdf];
char msg[];

void setup()
{
}

void loop()
{
char msg[] = "msgA" + "dataNumber" ;
}

Thanks
Adam

The great thing about Arduino is that in the time it took you to post this, you could have tried it.

(No)

1 Like

No chance this ever compiles... what is `asdf``? how many bytes do you want in the msg array?

you might want to have a look at the String class (but it comes with "strings attached")

2 Likes

What is it that you want to accomplish?

Not how to declare an array. Inside the square bracket goes the size or the index of an element of the array. A string is a special kind of an array and is declared differently.

You cannot build an array like that. You could, like @J-M-L says, use Strings, but those can be risky to use. Simple strings are safer at first. Use sprintf (or snprintf) or strcpy and strcat to build the array.

2 ways to build the array.

int dataNumber = 0;  // ********** what is this for? ***********
char msgA[] = "asdf";  // a string declared
char msgB[] = "datanumber";  // a string declared
char msg[16];  // MUST be big enough to hold both arrays and the terminator (\0)

void setup()
{
   Serial.begin(9600);
   // use sprintf to build a string
   sprintf(msg, "%s%s", msgA, msgB);
   Serial.print("msg after sprintf < ");
   Serial.println(msg);
   // use copy and concatenate to build the string
   msg[0] = '\0'; // empty the string
   strcpy(msg, msgA);
   strcat(msg, msgB);
   Serial.print("msg after copy, concatenate < ");
   Serial.println(msg);
}

void loop()
{
}
1 Like

looks like you may be confusing access to a variable by using a string: "dataNumber" vs a symbol: dataNumber

1 Like

Thanks.
actually I just like to have : char combined with a string.

I really wish I knew what that meant

1 Like

Is that right, OP?

Then like so?

   sprintf(msg, "%s%d", msgA, dataNumber);
   Serial.print("msg after sprintf < ");
   Serial.println(msg);

If not, kindly explain. Give an example of the output.

1 Like

FYI:
char msg[] = "msgA" + "dataNumber" ;
is not valid.

char msg[] = "msgA" "dataNumber" ;
is valid and equivalent to:
char msg[] = "msgAdataNumber" ;
(Adjacent string constants are merged.)

If you wanted 'msgA' to be a variable, you can do something similar with preprocessor macros:

#define msgA "asdf"
char msg[] = msgA "dataNumber" ;

After macro expansion, the second line becomes:
char msg[] = "asdf" "dataNumber" ;
which is equivalent to:
char msg[] = "asdfdataNumber" ;

1 Like

Thanks.
I have:

char msgQ[] = "Stationcommand"; 
String a= A1;
I like to have:
char msg[] = "StationcommandA1";

How to get it please.

Great!
Thanks you.
The dataNumber is variable and may be = abc or CDE

char msgQ[] = "Stationcommand"; 
String a= ASD;

I like to have:
char msg[] = "StationcommandASD";

Thanks

On say, a Uno, A1 has the value 15
I'm not sure you can assign that directly to a String

(Never tried)

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.