const char* concatenation

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

    Connecting two constant char pointer without using strcpy command in the dynamic allocation of memory:

    const char* one = "Hello ";
    const char* two = "World!";
    
    char* three = new char[strlen(one) + strlen(two) + 1] {'\0'};
    
    strcat_s(three, strlen(one) + 1, one);
    strcat_s(three, strlen(one) + strlen(two) + 1, two);
    
    cout << three << endl;
    
    delete[] three;
    three = nullptr;
    

提交回复
热议问题