我该如何将两个字符串相加?
我尝试了name = "derp" + "herp";
,但出现了错误:
表达式必须具有整数或枚举类型
malloc
动态分配它们。strcpy
或 strcat
来连接两个字符串。"foo" "bar"
,你可以将两个字符串字面值连接起来,从而创建字符串字面值“foobar”。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);