0

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.

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("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 *

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.

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

3
  • The first error includes the types int and int *, not int and int... Commented Feb 7, 2014 at 20:44
  • int is an integer type. int* is a pointer type. An object of type int contains an integer value. An object of type int* contains the address of some object of type int (or a null pointer that doesn't point to anything). Commented Feb 7, 2014 at 20:46
  • When you copy-and-pasted the error messages, some of the non-ASCII characters were rendered incorrectly (probably some conversion was incorrectly performed twice). I've cleaned it up a bit. Commented Feb 7, 2014 at 20:49

3 Answers 3

4

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

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:

printf("Rectangle Area is %d sq. units.", A); printf(" Diagonal length is %d.", D); 
Sign up to request clarification or add additional context in comments.

Comments

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)

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.

4 Comments

An int isn't an object, it's a primitive type. So it's incorrect where you say "int object".
@Gavin: int is a type. An int is an object whose type is int. An int* object or value points to an object of type int -- i.e., to an int object.
@KeithThompson I think it depends on your definition of "object". Technically in C, nothing's an object because it's not an object-oriented language. If we were looking at an object-oriented language, there is generally some wrapper class that wraps a primitive type in an object, but int itself would be a primitive type, and wouldn't be an object.
@Gavin: The C standard defines the word "object" as a "region of data storage in the execution environment, the contents of which can represent values". The C++ standard's definition is similar, and has nothing to do with object-oriented programming. Given int n;, int is a type, and n is the name of an object.
0

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

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

And to simplified printf() manual: 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*' 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.