错误代码:%d期望类型为int,但参数的类型为int *

时间:2022-03-28 23:04:28

I have an error code I do not understand:

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

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

I do not know the difference between int and int *. I did not know there were different types of int, and cannot find any note of it on webpages about printf and scanf key letters.

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

The code is as follows:

代码如下:

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

The error references the last two printf's:

该错误引用了最后两个printf:

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

Additionally, this program was originally written using floats (declaring X,Y,A, and D as float and using %f). But that gave an even stranger error code:
format %f expects type double, but argument 2 has type float *

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

I knew that %f is used for doubles and floats, so I could not understand why I had this error. After I got the error code about floats/doubles I tried changing everything to int (as shown in the above code), just to check. But that delivered the error code at the top of this post, which I do not understand either.

我知道%f用于双打和浮动,所以我无法理解为什么会出现这个错误。在我得到关于浮点数/双打的错误代码后,我尝试将所有内容更改为int(如上面的代码所示),只是为了检查。但是这个帖子的顶部提供了错误代码,我也不明白。

I've been using the gcc compiler. Would someone explain what's being done wrong?

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

3 个解决方案

#1


4  

The problem is that you're trying to pass pointers to the printf function. Here's what your code looks like:

问题是你试图将指针传递给printf函数。这是你的代码的样子:

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

A is the int variable, but &A is a pointer to the int variable. What you want is this:

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

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

#2


1  

int* means a pointer to an int object. this is what you get because you use & before the variable name (i.e &A in your code)

int *表示指向int对象的指针。这是你得到的,因为你在变量名之前使用&(即代码中的A和A)

You can read this to understand more about pointers and references, but basically if you omit the & before the variable names, it will work fine.

您可以阅读本文以了解有关指针和引用的更多信息,但基本上如果省略变量名之前的&,它将正常工作。

#3


0  

Why passing pointers to printf("...%d...", &D)?

为什么将指针传递给printf(“...%d ...”,&D)?

Take a look to pointers explanation: http://www.codeproject.com/Articles/627/A-Beginner-s-Guide-to-Pointers

看一下指针说明:http://www.codeproject.com/Articles/627/A-Beginner-s-Guide-to-Pointers

And to simplified printf() manual: http://www.cplusplus.com/reference/cstdio/printf/

并简化printf()手册:http://www.cplusplus.com/reference/cstdio/printf/

int d = 1;
printf("I'm an integer: %d", 42);           // OK, prints "...42"
printf("I'm an integer too: %d", d);        // OK, prints "...1"
printf("I'm a pointer, I have no idea why you printing me: %p", (void*)&d); // OK, prints "...<address of d>", probably not what you want

printf("I'm compile-time error: %d", &d);   // Fail, prints comiper error: 'int' reqired, but &d is 'int*'

#1


4  

The problem is that you're trying to pass pointers to the printf function. Here's what your code looks like:

问题是你试图将指针传递给printf函数。这是你的代码的样子:

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

A is the int variable, but &A is a pointer to the int variable. What you want is this:

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

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

#2


1  

int* means a pointer to an int object. this is what you get because you use & before the variable name (i.e &A in your code)

int *表示指向int对象的指针。这是你得到的,因为你在变量名之前使用&(即代码中的A和A)

You can read this to understand more about pointers and references, but basically if you omit the & before the variable names, it will work fine.

您可以阅读本文以了解有关指针和引用的更多信息,但基本上如果省略变量名之前的&,它将正常工作。

#3


0  

Why passing pointers to printf("...%d...", &D)?

为什么将指针传递给printf(“...%d ...”,&D)?

Take a look to pointers explanation: http://www.codeproject.com/Articles/627/A-Beginner-s-Guide-to-Pointers

看一下指针说明:http://www.codeproject.com/Articles/627/A-Beginner-s-Guide-to-Pointers

And to simplified printf() manual: http://www.cplusplus.com/reference/cstdio/printf/

并简化printf()手册:http://www.cplusplus.com/reference/cstdio/printf/

int d = 1;
printf("I'm an integer: %d", 42);           // OK, prints "...42"
printf("I'm an integer too: %d", d);        // OK, prints "...1"
printf("I'm a pointer, I have no idea why you printing me: %p", (void*)&d); // OK, prints "...<address of d>", probably not what you want

printf("I'm compile-time error: %d", &d);   // Fail, prints comiper error: 'int' reqired, but &d is 'int*'