Reputation: 1
I have an error in a C program. It says that %d requires int, but is reading int *. I used some mod10 functions before, to separate a 4 digit number. After manipulating each digit, with nothing more than division and other mod10's, I put the numbers back together as a 4 digit integer, and tell it to print it. With using %d in the printf line, i get an error back saying that %d is looking for an 'int', and it is finding an 'int *'. What exactly is an 'int *', and any ideas on what went wrong here?
Upvotes: 0
Views: 675
Reputation: 13558
An int *
is a pointer to an int
.
To get the value of a pointer to a variable use *
in front of the variable name.
To get the address of a variable use &
before it's name.
Example;
int c = 1, d= 0;
int *e = &c; // assign address of c to pointer e
d = *e; // assign value that c points to, to d
printf("%d",d); // print int value of d
printf("%d",*e); // same output as line above. print value c points to
Here is a little tutorial on pointers.
Upvotes: 3
Reputation: 7333
int *
is a pointer to an integer.
Please post the line of code that is flagged, the error message and the line that declares your variable.
Upvotes: 0
Reputation: 335
An int* is a pointer to an integer, instead of an integer variable. This means that instead of holding some data, it holds a reference to a location in memory where some data is residing. If you want to access the data at the location that it references then you need to dereference the pointer.
To do this you do this:
int a = 100; //an integer variable
int* b= &a; //b is now the location of a in memory
cout << *b; //This dereferences b to the data in a, and prints 100
Hope this helps, any corrections anyone please let me know.
Upvotes: 1
Reputation: 881403
An int*
is a pointer to an integer rather than an integer itself. C has the concept of pointers which are variables that can point to other variables, allowing you to do all sorts of wondrous things.
Without seeing your code, it's a little difficult to see what you've done wrong, but you can get a pointer by declaring it so:
int * pInt;
or by using the address-of operator:
doSomethingWith (&myInt);
Upvotes: 1
Reputation: 263257
Show us (by editing it into your question) the line of code it's complaining about.
To answer your question, an int*
is a pointer to an int
. For example, given
int i;
int *p;
i
and *p
are of type int
, and &i
and p
are of type int*
.
Look up "pointers" in your C textbook.
Also, the comp.lang.c FAQ is an excellent resource; section 4 discusses pointers. (But it's mostly intended to answer questions you'll have after you've started to understand the concepts.)
Upvotes: 0