0

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

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

    #include <stdio.h>
    #include <string.h>

    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; 
    }
4

3 回答 3

2

这条线

return input1, input2; 

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

void getinput(void);

int main (void)
{  ... }

void getinput (void)
{
   ...
}

我也建议看看

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

你也许是说只是

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

注意跳过所有空白的额外空白(例如前一个换行符)。

于 2013-07-20T18:49:43.913 回答
1

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

#include <stdio.h>
#include <string.h>

const char * getinput();

...

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

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

如果您想返回多个值,您可以返回数组(如果它们属于相似类型)或通过结构返回它们。

于 2013-07-20T18:45:53.960 回答
1
#include <stdio.h>
#include <string.h>

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); 
}
于 2013-07-20T19:30:57.460 回答