#include<iostream>
using namespace std;
void passPointer(int *pointer)
{
cout << *pointer;
}
int main()
{
开发者_StackOverflow int *iNum = new int(25);
passPointer(iNum);
return 0;
}
Can someone explain to me why when I use the passPointer()
function in main, it has to be passPointer(iNum)
but not passPointer(*iNum)
? Is it because I am dereferencing it at the parameter if I use *
? Please explain as detailed as you can as I am a bit confused.
Thanks guys.
It is because you have declared passPointer
to take an argument of
type int*
. iNum
has type int*
, and so can be passed directly to
passPointer
. *iNum
has type int
, and there is no implicit
conversion of int
to int*
, so you can't pass it to passPointer
.
More generally, in C++ (and in just about all other typed languages as
well), every expression and every variable has a type. The type of an
expression is expressed in terms of the type of its operands: if the
type of the operand of a unary *
is T*
(and the type must be a
pointer), then the type of the results is T
. And to call a function,
you must provide the right number of arguments, with the right types.
I am very sympathetic to these sorts of questions, because this is one of the only things that I had trouble with when learning C++.
The basic problem is that in the syntax of C++, the *
and &
characters are used for many different things with similar but subtly different meanings.
In your case, you are considering using *
in four different places.
In the first place: int *iNum = new int(25);
, the *
is sitting in a declaration. This means that is is a type annotation saying that iNum
is a pointer.
In the second place: passPointer(*iNum);
, the *
is sitting in an expression. This means that it is the dereference operator, which means: "get the value pointed to by iNum
". In this case the value pointed to by iNum
is an int
. As you will see later, passPointer
is declared to take an argument of type pointer to int
, so you cannot pass a plain int
as an argument to passPointer
. You should instead just pass iNum
(as iNum
is a pointer to int).
In the third place: void passPointer(int *pointer)
, the *
is again sitting in a declaration. This means that it has the same meaning as in the first place - it says that pointer
is a pointer (to int
).
In the fourth place: cout << *pointer;
, the *
is again sitting in an expression. This means that, as in the second case, it is saying "dereference pointer
and get the value that pointer
is storing the address of".
This creates variable of name iNum
and type int *
:
int *iNum = new int(25);
This accept parameter of type int *
:
void passPointer(int *pointer)
This passes the parameter of name iNum
and type int *
:
passPointer(iNum);
No need to think of pointers, these are all data types. Think of pointers only when you do pointer arithmetics and referencing, referencing. ;)
int****
is just a type.
Is it because I am dereferencing it at the parameter if I use
*
?
Yes. iNum
has type pointer to int
, *iNum
has type int
. If you had a function that takes an int
, then you could pass it *iNum
.
The passPointer()
function takes a pointer-to-int. iNum
is already a pointer-to-int, so you just pass it as-is. There is no need to deference it.
精彩评论