15

If I have two char arrays like so:

char one[200];
char two[200];

And I then want to make a third which concatenates these how could I do it?

I have tried:

char three[400];
strcpy(three, one);
strcat(three, two);

But this doesn't seem to work. It does if one and two are setup like this:

char *one = "data";
char *two = "more data";

Anyone got any idea how to fix this?

Thanks

ingh.am
  • 25,981
  • 43
  • 130
  • 177
  • 1
    The first one should work as well as long as you assign a string to the arrays first. You have just specified how long the strings can be, not assigned any value. – Chris Jul 24 '10 at 11:06
  • 1
    Are they char arrays, or are they strings? Strings are char arrays with a special convention that `'\0'` must exist and indicates the end of the string. To copy ordinary non-string char arrays, use `memmove`. – Pascal Cuoq Jul 24 '10 at 11:07
  • And by the way, if you really mean strings, then `399` is enough for the size of `three`. – Pascal Cuoq Jul 24 '10 at 11:08
  • @Pascal Cuoq: No 399 is not enough if one and two contains char not '\0' terminated strings (just some data). – Martin Ingvar Kofoed Jensen Jul 24 '10 at 11:19
  • @Martin Do you think the phrase "if you really mean strings" is not clear enough? What should I have said? – Pascal Cuoq Jul 24 '10 at 11:51

3 Answers3

8

If 'one' and 'two' does not contain a '\0' terminated string, then you can use this:

memcpy(tree, one, 200);
memcpy(&tree[200], two, 200);

This will copy all chars from both one and two disregarding string terminating char '\0'

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
4

strcpy expects the arrays to be terminated by '\0'. Strings are terminated by zero in C. Thats why the second approach works and first does not.

EricSchaefer
  • 25,272
  • 21
  • 67
  • 103
0

You can easily use sprintf

char one[200] = "data"; // first bit of data
char two[200] = "more data"; // second bit of data
char three[400]; // gets set in next line
sprintf(three, "%s %s", one, two); // this stores data
  • That's equivalent to OP's non-working `strcpy`/`strcat` code (except it also adds a space). It doesn't answer anything. – melpomene Mar 18 '17 at 10:57
  • @melpomene nope. it is not equivilant to that. it sets three to one and two joined together. and it works. ive used it many times and it brought me success. so dont be rude about it and accept that that is a fine working answer! – OverloadedCore Mar 18 '17 at 12:16
  • "*it sets three to one and two joined together. and it works.*" - Just like OP's code. How is it not equivalent to `strcpy(three, one); strcat(three, two);`? – melpomene Mar 18 '17 at 14:31
  • @melpomene it isnt equivilant as it works and the other doesnt / only works for pointers. if it is a valid answer you should not be down voting it – OverloadedCore Mar 18 '17 at 17:10
  • You are wrong, but I don't understand exactly how. If you don't believe me, try `char one[200] = "data"; char two[200] = "more data"; char three[400]; strcpy(three, one); strcat(three, two); printf("%s\n", three);`. Your comment "*only works for pointers*" doesn't make sense to me. – melpomene Mar 19 '17 at 00:22
  • You're showing code that does exactly the same as what OP posted. I'm only saying it doesn't work because that's what OP claims: "*But this doesn't seem to work.*" Presumably OP is working with raw char arrays, not C strings. OP's question was how to fix their code; your code doesn't answer that because it doesn't change anything. If OP's code isn't working for them, neither is this answer. – melpomene Mar 19 '17 at 10:33
  • @melpomene the thing is: my code works and it works with CHAR ARRAYS. so that means it is a valid answer. it may do the SAME as what OP did but it works! simple – OverloadedCore Mar 19 '17 at 21:41
  • No, that means you don't understand OP's problem. – melpomene Mar 19 '17 at 21:46
  • @melpomene the problem is that OP wants to join to char arrays together – OverloadedCore Mar 19 '17 at 21:48
  • But that's not what your code does. Your code joins two strings (which make up parts of the two arrays, up to a `'\0'`); it doesn't use all elements of the arrays. – melpomene Mar 20 '17 at 07:28
  • @melpomene yes it does – OverloadedCore Mar 20 '17 at 17:56