我是C语言的初学者。我正在尝试使用fgets()
编写一个程序,根据用户输入的3个整数计算体积,但我无法理解为什么我的代码不起作用。
#include <stdio.h>
#include <stdlib.h>
int volumn(int a, int b, int c);
int main(int argc, char* argv[]){
char* height, width, depth;
fgets(&height, 10, stdin);
fgets(&width, 10, stdin);
fgets(&depth, 10, stdin);
printf("\nThe volumn is %d\n", volumn(atoi(&height), atoi(&width), atoi(&depth)));
return 0;
}
int volumn(int a, int b, int c){
return a * b * c;
}
编辑: 当我运行上面的代码时,我会得到以下错误/警告:
goodbyeworld.c:8:11: warning: incompatible pointer types passing 'char **' to
parameter of type 'char *'; remove & [-Wincompatible-pointer-types]
fgets(&height, 10, stdin);
^~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/stdio.h:238:30: note:
passing argument to parameter here
char *fgets(char * __restrict, int, FILE *);
^
goodbyeworld.c:12:48: warning: incompatible pointer types passing 'char **' to
parameter of type 'const char *'; remove & [-Wincompatible-pointer-types]
printf("\nThe volumn is %d\n", volumn(atoi(&height), atoi(&width), a...
^~~~~~~
/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/stdlib.h:132:23: note:
passing argument to parameter here
int atoi(const char *);
^
2 warnings generated.
-Wall -Wextra -pedantic
),然后修复警告。使用发布的代码,您将看到两个警告:1)未使用的变量'argc' 2)未使用的变量'argv []'以及您已经看到的警告。 - user3629249