gpt4 book ai didi

c++ - const char* 连接

转载 作者:IT老高 更新时间:2023-10-28 11:53:24 25 4
gpt4 key购买 nike

我需要像这样连接两个 const 字符:

const char *one = "Hello ";
const char *two = "World";

我该怎么做呢?

我是从具有 C 接口(interface)的第三方库传递这些 char* 的,因此我不能简单地使用 std::string 来代替。

最佳答案

在您的示例中 onetwo 是 char 指针,指向 char 常量。您不能更改这些指针指向的 char 常量。所以像:

strcat(one,two); // append string two to string one.

不会工作。相反,您应该有一个单独的变量(char 数组)来保存结果。像这样的:

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.

关于c++ - const char* 连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1995053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com