其他分享
首页 > 其他分享> > c-如何连接const char数组和char数组指针?

c-如何连接const char数组和char数组指针?

作者:互联网

直接从事业务:我的代码大致如下所示:

char* assemble(int param)
{
    char* result = "Foo" << doSomething(param) << "bar";
    return result;
}

现在我得到的是:

error: invalid operands of types ‘const char [4]’ and ‘char*’ to binary ‘operator<<’

编辑:
doSomething返回一个char *.

那么,如何将这两个连接起来?

附加信息:
编译器:GNU / Linux 2.6.32-5-amd64上的g 4.4.5

解决方法:

“ Foo”和“ Bar”是文字,它们没有插入(<<)运算符. 如果要进行基本串联,则需要使用std :: string:

std::string assemble(int param)
{
    std::string s = "Foo";
    s += doSomething(param); //assumes doSomething returns char* or std::string
    s += "bar";
    return s;
}

标签:c,arrays,string,concatenation
来源: https://codeday.me/bug/20191012/1902893.html