In C++, what is the difference between:
void func(MyType&); // declaration
//...
MyType * ptr;
func(*ptr); // compiler doesnt give error
func(ptr); // compiler gives error i thought & represents memory address so
// this statement should correct as ptr is only a pointer
// or address of some real var.
The unary prefix operator &
, when applied to an object, yields the address of the object: &obj
.
The type modifier &
, when applied to a variable about to be declared, will modify the variable's type to be a reference type: int&
.
The same applies to *
: When applied as a unary prefix operator to a pointer, it will dereference the pointer, yielding the object referred to: *ptr
.
When used as a type modifier to a variable about to be declared, *
will modify the type to be a pointer: int*
.
In a similar way, the type modifier []
applied to a variable that's being declared will modify the variable's type to an array, while the binary infix operator []
applied to an object of array type will access one of the array's sub-objects.
It's not helpful that type modifiers apply to the variable that is declared, not to the type they are declared with. For example, this
int *p, **pp, i, a[10], &r = i;
defines an int
pointer, a pointer to a pointer to an int
, a vanilla int
, an array of 10 int
, and an int
reference. (The latter is immediately initialized, because you cannot have an uninitialized reference.) Note that the type modifiers syntactically belong to the declared variable whose type they are modifying, not to the declared variable's type. Nevertheless, type modifiers (*
and &
) modify the type of the variable.
In the following case, however, with p
, i
, and a
presumed to be variables that have already been declared
*pp = &i;
a[0] = i;
*
and &
are unary prefix operators dereferencing pp
and yielding the address of i
, while []
yields the first int
object in the array a
.
The fact that C and C++ don't care about the whitespaces around type modifiers and that this led to different camps when it comes to place them doesn't really make things easier.
Some people place the type modifiers close to the type. They argue that it modifies the type and so it should go there:
int* ptr;
The disadvantage is that this becomes confusing when declaring several objects. This
int* a, b;
defines a
to be a pointer to int
, but b
to be an int
. Which is why some people prefer to write
int *ptr;
int *a, *b;
I suggest to simply never declare multiple objects in the same statement. IMO that makes code easier to read. Also, it leaves you free to pick either convention.
To complicate things even further, besides the type modifiers and the unary prefix operators &
and *
, there are also the binary infix operators &
and *
, meaning "bitwise AND" and "multiplication". And to add insult to injury, in C++ you can overload both the unary prefix and the binary infix variants of these operators (and the binary infix []
) for user-defined types and be completely free as to their semantics.