在c中得到函数错误。 试图返回字符串值。

我试图使用getinput函数返回用户输入的字符串值。 但是我得到了’getinput’的冲突类型的错误2.先前的’getinput’隐式声明就在这里。 有人可以向我解释这些错误是什么吗?

gets函数应该从用户读取两个不同的句子并将其存储在变量userinput1和userinput2中。

#include  #include  char input1[1000] = {0}; char input2[1000] = {0}; int main(){ getinput(); char input[2000]; sprintf(input, "%s %s", input1, input2); printf("%s\n", input); return 0; } const char * getinput() { printf("please enter the something\n"); scanf("%999[^\n]%*c", input1); printf("please enter the next input\n"); scanf("%999[^\n]%*c", input2); return input1, input2; } 

这条线

 return input1, input2; 

使用逗号运算符并返回input2 。 由于您已将input1input2声明为文件范围变量,因此无需返回它们 – 它们在main()getinput() 。 删除返回行并使用

 void getinput(void); int main (void) { ... } void getinput (void) { ... } 

我也建议看看

 scanf("%999[^\n]%*c", input2); 

你或许只是意味着

 scanf(" %999[^\n]", input2); 

请注意额外的空白,它会跳过所有空格(例如之前的换行符)。

在代码顶部添加getinput()函数声明

 #include  #include  const char * getinput(); ... 

如果编译器没有看到函数声明,则假定它返回int ,但是你的函数实际上返回char * ,因此这样的错误/警告。

此外,您不能在C返回多个值。 考虑到您的代码,您不需要返回input1input2因为它们是全局变量。

如果要返回多个值,则返回数组(如果它们的类型相似)或通过结构返回它们。

 #include  #include  char input1[1000] = {0}; char input2[1000] = {0}; const char * getinput(); int main(){ getinput(); char input[2000]; sprintf(input, "%s %s", input1, input2); printf("%s\n", input); return 0; } const char * getinput() { printf("please enter the something\n"); scanf("%999[^\n]%*c", input1); printf("please enter the next input\n"); scanf("%999[^\n]%*c", input2); }