const char* concatenation

前端 未结 12 2206
抹茶落季
抹茶落季 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:32

    In your example one and two are char pointers, pointing to char constants. You cannot change the char constants pointed to by these pointers. So anything like:

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

    will not work. Instead you should have a separate variable(char array) to hold the result. Something like this:

    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.
    

提交回复
热议问题