How do I concatenate two strings in C?

后端 未结 11 1194
春和景丽
春和景丽 2020-11-22 16:00

How do I add two strings?

I tried name = \"derp\" + \"herp\";, but I got an error:

Expression must have integral or enum type

11条回答
  •  感动是毒
    2020-11-22 17:01

    using memcpy

    char *str1="hello";
    char *str2=" world";
    char *str3;
    
    str3=(char *) malloc (11 *sizeof(char));
    memcpy(str3,str1,5);
    memcpy(str3+strlen(str1),str2,6);
    
    printf("%s + %s = %s",str1,str2,str3);
    free(str3);
    

提交回复
热议问题