const char* concatenation

前端 未结 12 2204
抹茶落季
抹茶落季 2020-11-29 18:09

I need to concatenate two const chars like these:

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

How might I go about doing tha

12条回答
  •  情话喂你
    2020-11-29 18:34

    The C way:

    char buf[100];
    strcpy(buf, one);
    strcat(buf, two);
    

    The C++ way:

    std::string buf(one);
    buf.append(two);
    

    The compile-time way:

    #define one "hello "
    #define two "world"
    #define concat(first, second) first second
    
    const char* buf = concat(one, two);
    

提交回复
热议问题