C中的参数类型错误
发布时间:2020-12-16 10:05:06 所属栏目:百科 来源:网络整理
导读:我应该向用户询问一个3位数字,然后用该数字替换每个数字加上6个模数10,然后将所有更新数字相加.当我运行程序时,它在输入数字后崩溃,警告: format %d expects argument of type ‘int *’ but argument 2 has type int. 这是我的源代码: #include stdio.hin
我应该向用户询问一个3位数字,然后用该数字替换每个数字加上6个模数10,然后将所有更新数字相加.当我运行程序时,它在输入数字后崩溃,警告:
这是我的源代码: #include <stdio.h> int main(int argc,char* argv[]) { int Integer; int Divider; int Digit1,Digit2,Digit3; printf("Enter a three-digit integer: "); scanf("%d",Integer); Divider = 1000; Digit1 = Integer / Divider; Integer = Integer % Divider; Divider = 100; Digit2 = Integer / Divider; Integer = Integer % Divider; Divider = 10; Digit3 = Integer / Divider; Digit1 = (Digit1 + 6) % 10; Digit2 = (Digit2 + 6) % 10; Digit3 = (Digit3 + 6) % 10; printf(Digit3 + Digit1 + Digit2); getch(); return 0; } 更新 在我们的示例输出中,如果用户输入928,则结果数应为584.我不确定该数字的编号.它应该用该数字的总和加上6模数10来替换每个数字.那么我的代码中是否存在数学错误? 解决方法
几个地方:
scanf("%d",&Integer); printf("%dn",Digit3 + Digit1 + Digit2); getchar(); // maybe?? 实际上,编译器会为您清楚地指出它们: warning: format ‘%d’ expects argument of type ‘int *’,but argument 2 has type ‘int’ [-Wformat=] scanf("%d",Integer); ^ warning: passing argument 1 of ‘printf’ makes pointer from integer without a cast [enabled by default] printf(Digit3 + Digit1 + Digit2); ^ warning: implicit declaration of function ‘getch’ [-Wimplicit-function-declaration] getch(); ^ 注意,关于getch,你可以在这里阅读:implicit declaration of function ‘getch’. 完成这些修复后,您的程序运行没有错误.让我调用固定源文件z.c, gcc -std=gnu99 -O2 -o z z.c ./z 如果我输入304,我得到21. 附加说明: 你想要Divider是100,10,1还不是1000,100,10? (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |