C 语言:收到错误:警告:格式“%d”需要“int *”类型的参数,但参数 2 的类型为“int **”[-Wformat=]
Posted
技术标签:
【中文标题】C 语言:收到错误:警告:格式“%d”需要“int *”类型的参数,但参数 2 的类型为“int **”[-Wformat=]【英文标题】:C language: received error: warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int **’ [-Wformat=] 【发布时间】:2021-11-14 11:54:46 【问题描述】:仅限 C 语言。
当我运行此代码时,我在第 7 行收到错误消息:
warning: format ‘%d’ expects argument of type ‘int *’, but argument 2 has type ‘int **’ [-Wformat=]
另外,我在第 8 行收到错误消息:
warining: function returns address of local variable [-Wreturn-local-addr]
我附上了一张我想要输出的图片。有人可以显示更正的代码吗,不胜感激,谢谢!
#include <stdio.h>
// Method getFraction() asks for numerator and denominator
int *getFraction()
int *n[2]; // declare array of pointers to return the numerator and
// denominator
printf("\n Enter the numerator and the denominator -- ");
scanf("%d%d", (n + 0), (n + 1)); // read numerator and denominator
return n; // return n
// method smallest() takes twointegers as its parameter and return the smallest
int smallest(int a, int b)
if (a < b) // condition for a is smallest
return a; // return a
else
return b; // return b
// recursive method to return the GCD() of two numbers
int gcd(int a, int b)
if (b == 0) // if denominator is 0 then return a
return a;
// recursively call to gcd()
return gcd(b, a % b);
// method reduce() takes two parameters as call by reference and
// reduce the fraction
void reduce(int *a, int *b)
int g;
// call to gcd()
g = gcd(*a, *b);
// reduce numerator
*a = *a / g;
// reduce denominator
*b = *b / g;
// driver code
int main()
int *num, a, b, small, g;
char ch;
// loop to repeat the process till user wants
do
num = getFraction(); // call to getFraction();
a = *(num + 0); // assign numerator to a
b = *(num + 1); // assign denominator to b
// condition for denominator not zero
if (b != 0)
reduce(&a, &b); // call to reduce() as call by reference
// print the reduced fraction
printf("\n The reduced fraction is -- %d / %d", a, b);
else printf("\n Denominator should not be zero.");
// ask user to repeat more
printf("\n Try again (Y/N) -- ");
fflush(stdin);
scanf("%c", &ch); // read users choice
while (ch == 'y' || ch == 'Y');
return 0;
【问题讨论】:
为什么是int *n[2];
而不是int n[2];
?你永远不会分配内存,所以读入n[0]
和n[1]
会使程序有未定义的行为
请正确格式化您的代码,以便可以按原样复制并成功编译。目前,它的格式很差。
你传递n
这是数组本身的地址(不是它未初始化的指针内容)。建议你重构一下函数,比如void getFraction(int n[2])
【参考方案1】:
#include <stdio.h>
// Method getFraction() asks for numerator and denominator
int *getFraction()
int n[2];// declare array of pointers to return the numerator and
// denominator
int *n1=n;
printf("\n Enter the numerator and the denominator -- ");
scanf("%d%d", &n[0], &n[1]); // read numerator and denominator
return n1; // return n
// method smallest() takes twointegers as its parameter and return the smallest
int smallest(int a, int b)
if (a < b) // condition for a is smallest
return a; // return a
else
return b; // return b
// recursive method to return the GCD() of two numbers
int gcd(int a, int b)
if (b == 0) // if denominator is 0 then return a
return a;
// recursively call to gcd()
return gcd(b, a % b);
// method reduce() takes two parameters as call by reference and
// reduce the fraction
void reduce(int *a, int *b)
int g;
// call to gcd()
g = gcd(*a, *b);
// reduce numerator
*a = *a / g;
// reduce denominator
*b = *b / g;
// driver code
int main()
int *num, a, b, small, g;
char ch;
// loop to repeat the process till user wants
do
num = getFraction(); // call to getFraction();
a = *(num + 0); // assign numerator to a
b = *(num + 1); // assign denominator to b
// condition for denominator not zero
if (b != 0)
reduce(&a, &b); // call to reduce() as call by reference
// print the reduced fraction
printf("\n The reduced fraction is -- %d / %d", a, b);
else printf("\n Denominator should not be zero.");
// ask user to repeat more
printf("\n Try again (Y/N) -- ");
fflush(stdin);
scanf("%c", &ch); // read users choice
while (ch == 'y' || ch == 'Y');
return 0;
【讨论】:
成功了!谢谢! @yessirblender 由于这个答案有帮助,请通过单击灰色复选标记来考虑accepting。【参考方案2】:你在第 8 行得到一个错误,因为在你的函数返回它的值堆栈弹出局部变量之后,我们无法访问它们的地址,因此你返回的东西可能会导致你的操作系统被杀死。编译器在构建时出错。
【讨论】:
以上是关于C 语言:收到错误:警告:格式“%d”需要“int *”类型的参数,但参数 2 的类型为“int **”[-Wformat=]的主要内容,如果未能解决你的问题,请参考以下文章
警告:格式“%d”需要“int”类型的参数,但参数 2 的类型为“long int”[-Wformat=]