Concatenate two char* strings in a C program

后端 未结 6 1876
梦如初夏
梦如初夏 2020-12-15 06:38

I wrote the following C program:

int main(int argc, char** argv) {

    char* str1;
    char* str2;
    str1 = \"sssss\";
    str2 = \"kkkk\";
    printf(\"%         


        
6条回答
  •  一个人的身影
    2020-12-15 07:21

    strcat(str1, str2) appends str2 after str1. It requires str1 to have enough space to hold str2. In you code, str1 and str2 are all string constants, so it should not work. You may try this way:

    char str1[1024];
    char *str2 = "kkkk";
    strcpy(str1, "ssssss");
    strcat(str1, str2);
    printf("%s", str1);
    

提交回复
热议问题