gpt4 book ai didi

c++ - 加入 2 const char*s c++

转载 作者:太空狗 更新时间:2023-10-29 23:23:24 26 4
gpt4 key购买 nike

如何将两个 const char* 组合成第三个?

我正在尝试使用这段代码来做到这一点:

const char* pName = "Foo"
printf("\nMy name is %s.\n\n\n",pName);
const char* nName;
int num_chars = asprintf(&nName, "%s%s", "Somebody known as ", pName);

但是我得到这个错误:

'asprintf': identifier not found

我通过这段代码包含了 stdio.h:

#include <stdio.h>

最佳答案

很简单,用C++就可以了:

const char* pName = "Foo"
std::string name("Somebody known as ");
name += pName;

const char* nName = name.c_str();

关于c++ - 加入 2 const char*s c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11550885/

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