我在C中用两个函数制作了一些代码。
一个函数查找一个单词(一组字符),然后将其保存为字符串。另一个函数显示了字符串中的内容。我找不到将返回值保存到字符串的正确方法。
这是我的代码:
#include <stdio.h>
char SchrijfString(void);
void LeesString(char);
int main(void)
{
char x[60];
x = SchrijfString(x);
LeesString(x);
return 0;
}
char SchrijfString(char x[])
{
printf("geef een string in: \n");
gets(x);
return x;
}
void LeesString(char x[])
{
printf("In de string zit:\n %s", x);
getchar();
}
答案 0 :(得分:0)
gets
函数已经使用用户指定的字符串填充x
,因此无需从SchrijfString
返回任何内容或向x
返回任何内容主要。您的函数原型与定义不匹配。他们需要是一样的。
此外,gets
已弃用,因为它不安全。请改用fgets
。
void SchrijfString(char x[], int len);
void LeesString(char x[]);
int main(void)
{
char x[60];
SchrijfString(x, sizeof(x));
LeesString(x);
return 0;
}
void SchrijfString(char x[], int len)
{
printf("geef een string in: \n");
fgets(x, len, stdin);
}