C中的Strcpy和strcat函数

C中的Strcpy和strcat函数,c,visual-studio-2015,C,Visual Studio 2015,我在VisualStudio2015中使用C编程语言,我只是试图提示用户输入三句话的文本,然后将它们组合成一个三句话的段落。我就是不能让strcpy和strcat函数工作。 想法 提前非常感谢 #include <string.h> #include "stdafx.h" #include <stdio.h> #include <stdlib.h> #include <ctype.h> #define MASTERSIZE 300 int mai

我在VisualStudio2015中使用C编程语言,我只是试图提示用户输入三句话的文本,然后将它们组合成一个三句话的段落。我就是不能让strcpy和strcat函数工作。 想法

提前非常感谢

#include <string.h>
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define MASTERSIZE 300

int main()
{

char *calcTotalMessage(char[100], char[100], char[100]);
#define MSIZE 100
#define MSIZEE 100
#define MSIZEEE 100

int read;
char message[MSIZE];
char m2[MSIZE];
char m3[MSIZE];
char* totalM;

printf("Enter a sentence:");

scanf_s("%s", &message);

printf("Enter another sentence:");
scanf_s("%s", &m2);

printf("Enter third sentence:");
scanf_s("%s", &m3);

totalM = calcTotalMessage(message, m2, m3);
printf(totalM);

return 0;
}

char *calcTotalMessage(char *m1, char *m2, char *m3)
{
void strcat(char, char);
void strcpy(char, char);
char *totalM = "";

strcpy(*totalM, *m1);
strcat(*totalM, *m2);
strcat(*totalM, *m3);

return totalM;

}
#包括
#包括“stdafx.h”
#包括
#包括
#包括
#定义母版尺寸300
int main()
{
char*calcTotalMessage(char[100],char[100],char[100]);
#定义MSIZE 100
#定义MSIZEE 100
#定义msizeee100
int-read;
字符消息[MSIZE];
字符m2[MSIZE];
煤焦m3[MSIZE];
char*totalM;
printf(“输入一个句子:”);
扫描(“%s”和消息);
printf(“输入另一个句子:”);
扫描单位(“%s”和平方米);
printf(“输入第三句:”);
扫描单位(“%s”和m3);
totalM=calcTotalMessage(消息,m2,m3);
printf(totalM);
返回0;
}
char*calcTotalMessage(char*m1、char*m2、char*m3)
{
无效strcat(char,char);
无效strcpy(char,char);
char*totalM=“”;
strcpy(*总计m,*m1);
strcat(*总平方米,*m2);
strcat(*总立方米,*m3);
返回totalM;
}
所以totalM指向一个字符串文本。C标准不允许修改文本。您不一定会得到编译时错误,但您的程序有未定义的行为。这不太可能表现正常

strcpy(*totalM, *m1);
然后尝试传递字符(类型为
*totalM
*m1
),而不是指针。该字符被转换为您试图写入的一些无意义的指针值。这同样会导致未定义的行为。编译器甚至试图警告您,但您没有注意这些错误,而是为不存在的函数添加了声明(
strcpy(char,char)

我建议您将输出缓冲区传递到
calcTotalMessage
中,而不是返回它

void calcTotalMessage(char const *m1, char const *m2, char const *m3, char *output) {
    output[0] = '\0';
    strcpy(output, m1);
    strcat(output, m2);
    strcat(output, m3);
}
这样称呼:

char totalM[MSIZE + MSIZEE + MSIZEEE] = {'\0'};
calcTotalMessage(message, m2, m3, totalM);

关于风格的观点。除了文件范围外,您通常不会在任何地方看到函数声明。所以不要太习惯于在另一个函数范围内声明函数

所以totalM指向一个字符串文本。C标准不允许修改文本。您不一定会得到编译时错误,但您的程序有未定义的行为。这不太可能表现正常

strcpy(*totalM, *m1);
然后尝试传递字符(类型为
*totalM
*m1
),而不是指针。该字符被转换为您试图写入的一些无意义的指针值。这同样会导致未定义的行为。编译器甚至试图警告您,但您没有注意这些错误,而是为不存在的函数添加了声明(
strcpy(char,char)

我建议您将输出缓冲区传递到
calcTotalMessage
中,而不是返回它

void calcTotalMessage(char const *m1, char const *m2, char const *m3, char *output) {
    output[0] = '\0';
    strcpy(output, m1);
    strcat(output, m2);
    strcat(output, m3);
}
这样称呼:

char totalM[MSIZE + MSIZEE + MSIZEEE] = {'\0'};
calcTotalMessage(message, m2, m3, totalM);


关于风格的观点。除了文件范围外,您通常不会在任何地方看到函数声明。因此,不要太习惯于在另一个函数作用域中声明函数。

出现了什么问题以及如何解决?描述问题。出现了什么问题以及如何解决?描述问题。