The *
in int* reat(int *n)
indicates in the return that this function is returning a pointer to an integer value rather than the integer value itself. The *
indicates in the argument list that this function also wants a pointer to an integer value rather than a "raw" integer value for its argument.
For example,
int x = reat(n); // assume n is a pointer to an int
probably won't compile on most systems. If it does, you'll be storing a memory address in x
rather than the integer value you were expecting. Instead write
int *x = reat(n)
to store in x
the pointer (to some integer value) returned by reat()
. Consider this function:
int addone(int x) {
return 1 + x;
}
This function takes an integer value for its argument. To access the value of the integer pointed to by the return from reat()
and use it in addone()
, we'll need to call it like so:
int *x = reat(n)
addone(*x)
Thereby dereferencing x
with the *
operator to access the integer value it points to. addone(x)
won't compile because x
, without dereferencing, is a memory address and not an integer.
Understanding when the *
is being used to define a pointer and when its being used to dereference one will become second nature over time. Trust that any time it shows up in a function definition, whether as an argument or a return, it indicates a pointer is being used.