Combine CHARs into a CHAR array

How would I go about combining two or more individual characters into a character array?

ex.
1
2
3
4
5
char x = d;
char y = f;
char xy[4];
xy = x + y;
cout << xy << endl;

df


thanks
Last edited on
I assume you want a zero-terminated char array (i.e. a C-style string).

1
2
3
4
5
6
7
8
9
char x = 'd';
char y = 'f';
char xy[4];

xy[0] = x;
xy[1] = y;
xy[2] = 0;

cout << xy << endl;


Edit: Added missing ' - good spot, Vlad!
Last edited on
First of all you shall write

char x = 'd';
char y = 'f';

char is a integral type. So x + y is an arithmetic expression. You may assign values to elements of a character array

xy[0] = x;
xy[1] = y;

If you want do display the character array as a string you should append a zero byte

xy[2] = '\0';
cout << xy << endl;


char xy[4];
xy = x + y;
cout << xy << endl;
Thanks for the help!
Topic archived. No new replies allowed.