我需要连接两个 const 字符,如下所示:
const char *one = "Hello ";
const char *two = "World";
我该怎么做呢?
我通过了这些char*
来自具有 C 接口的第三方库,所以我不能简单地使用std::string
反而。
在你的例子中one and two是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.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)