连接两个char数组?

如果我有两个char数组,如下所示:

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

然后我想制作第三个连接这些我怎么能这样做?

我试过了:

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

但这似乎不起作用。 如果onetwo设置如下:

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

任何人都知道如何解决这个问题?

谢谢

如果’one’和’two’不包含’\ 0’终止字符串,那么您可以使用:

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

这将从一个和两个复制所有字符,忽略字符串终止字符’\ 0′

strcpy期望数组以’\ 0’结尾。 字符串在C中以零结尾。这就是为什么第二种方法有效并且首先没有。

您可以轻松使用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