* and & as type modifiers
int ideclares an int.int* pdeclares a pointer to an int.int& r = ideclares a reference to an int, and initializes it to refer toi.
C++ only. Note that references must be assigned at initialization, thereforeint& r;is not possible.
Similarly:
void foo(int i)declares a function taking an int (by value, i.e. as a copy).void foo(int* p)declares a function taking a pointer to an int.void foo(int& r)declares a function taking an int by reference. (C++ only)
* and & as operators
foo(i)callsfoo(int). The parameter is passed as a copy.foo(*p)dereferences the int pointerpand callsfoo(int)with the int pointed to byp.foo(&i)takes the address of the intiand callsfoo(int*)with that address.
(tl;dr) So in conclusion, depending on the context:
-
*can be either the dereference operator or part of the pointer declaration syntax. -
&can be either the address-of operator or (in C++) part of the reference declaration syntax.Note that
*may also be the multiplication operator, and&may also be the bitwise AND operator.