gpt4 book ai didi

c - 错误代码 : %d expected type int, 但参数类型为 int *

转载 作者:太空宇宙 更新时间:2023-11-04 05:59:29 25 4
gpt4 key购买 nike

我有一个我不明白的错误代码:

format %d expects type int, but argument 2 has type int *

我不知道intint * 之间的区别。我不知道 int 有不同的类型,并且在有关 printfscanf 关键字母的网页上找不到任何关于它的注释。

代码如下:

#include <stdio.h>
#include <math.h>

int main(void)
{
int X = 0, Y = 0, A = 0, D = 0;
printf("This program computes the area of a rectangle ");
printf("and its diagonal length.");
printf("Enter Side 1 dimentions: ");
scanf("%d", &X);
printf("Enter Side 2 dimentions: ");
scanf("%d", &Y);

/* Calc */
A = X * Y;
D = pow(X,2) + pow(Y,2);
D = pow(D, 1 / 2);

/* Output */
printf("Rectangle Area is %d sq. units.", &A);
printf(" Diagonal length is %d.", &D);
return 0;
}

错误引用了最后两个 printf:

printf("Rectangle Area is %d sq. units.", &A);  
printf(" Diagonal length is %d.", &D);

此外,该程序最初是使用 float 编写的(将 X、Y、A 和 D 声明为 float 并使用 %f)。但这给出了一个更奇怪的错误代码:
格式 %f 期望类型为 double,但参数 2 的类型为 float *

我知道 %f 用于 double 和 float ,所以我不明白为什么会出现此错误。在我得到关于 floats/doubles 的错误代码后,我尝试将所有内容更改为 int(如上面的代码所示),只是为了检查。但这在这篇文章的顶部提供了错误代码,我也不明白。

我一直在使用 gcc 编译器。有人会解释哪里做错了吗?

最佳答案

问题是您试图将指针传递给 printf 函数。您的代码如下所示:

printf("Rectangle Area is %d sq. units.", &A);  
printf(" Diagonal length is %d.", &D);

A 是 int 变量,而 &A 是指向 int 变量的指针。你想要的是这个:

printf("Rectangle Area is %d sq. units.", A);  
printf(" Diagonal length is %d.", D);

关于c - 错误代码 : %d expected type int, 但参数类型为 int *,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21637358/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com