别人字符串相加的程序我无法运行

别人字符串相加的程序我无法运行
#include

int main(void)
{
char* str1 = "Hello"; //(1)
char* str2 = "my";
char* str3 = "program1";
char* temp; //用于储存最终生成的字符串

temp = (char*)malloc(sizeof(char) * 50);    //申请内存空间,此处申请大小为50个字符(char)的大小 (2)
strcpy(temp, telNum);        //(3)

strcat(temp, telId);
strcat(temp, send_data);

printf(temp);

free(temp);        //(4)释放内存

return 0;

}

img

你这telNum,telId,send_data变量从哪来的啊???你只有str1,str2,str3啊

不应该是定义char数组吗,你这是定义的char指针


#include<iostream>
#include<cstring>
using namespace std;
int main()
{
    char str1[10] = "hello";
    char str2[10] = "my";
    char str3[10] = "program1";
    char temp1[30];
    strcpy_s(temp1,str1);
    strcat_s(temp1,str2);
    strcat_s(temp1, str3);
    cout << temp1;
    return 0;
}

你上面的代码不够完整,可以试试这个,并且现在strcpy和strcat都不安全了,可以用strapy_s和strcat_s;