我需要将两个这样的const字符连接起来:
const char *one = "Hello ";
const char *two = "World";
我该怎么做呢?
我从带有C接口(interface)的第三方库传递了这些
char*
,所以我不能简单地使用std::string
。 最佳答案
在您的示例中,一个和两个是char指针,它们指向char常量。您不能更改这些指针指向的char常量。所以像这样:
strcat(one,two); // append string two to string one.
不管用。相反,您应该有一个单独的变量(字符数组)来保存结果。像这样:
char result[100]; // array to hold the result.
strcpy(result,one); // copy string one into the result.
strcat(result,two); // append string two to the result.