The Use of * and & in C/C++ by Peter Meyer
C++ is a superset of C. Almost all valid C programs are valid C++ programs. It is thus possible to write a C++ program which uses nothing in C++ which is not in C, although the use of cout or of references (see below) produces a C++ program. C++ and C occasionally differ in style, e.g. to declare p as a pointer to an int variable C uses int *p whereas C++ uses int* p (the compiler doesn't care which you use).
In C/C++ the characters * and & are used in a variety of ways. The meaning has to be inferred from the context.
The use of * in C
(a) Multiplication: x = y*z;
(b) Multiply-assign: x *= y; Means the same as: x = x*y;
(c) Comments: /* your comment here */
(d) Pointer declarations: int *p; or int* p; Read: p is a pointer to an int.
(e) Compound pointers: int **p; or int** p; Read: p is a pointer to a pointer to an int.
(Also int ***p; and so on.)
(f) De-referencing: x = *p; Read: Assign to x the value pointed to by p.
The use of & in C
(a) Logical-and: if ( ( a>1 ) && (b<0) ) ...
(b) Bitwise-and: x = a&b; Corresponding bits are and'ed (e.g. 0&1 -> 0)
(c) Bitwise-and-assign: x &= y; Means the same as: x = x&y;
(d) Address-of operator: p = &x; Read: Assign to p (a pointer) the address of x.
The additional use of & (in parameters) in C++
C++ uses a type of variable called a "reference" variable (or simply a "reference") which is not available in C (although the same effect can be achieved using pointers).
References, pointers and addresses are closely related concepts. Addresses are addresses in computer memory (typically the address in memory where the value of some variable is stored), e.g. (in hexadecimal) 0xAB32C2. Pointers are variables which hold addresses, and so "point to" memory locations (and thus to the values of variables). Conceptually, reference variables are basically pointers by another name (but may not be instantiated as such by the compiler).
It is possible to declare a reference within a function, like other variables, e.g.
int& r = i;
but this is pointless, since the use of the reference is equivalent to the use of the variable it references.
References are designed to be used as parameters (arguments) to functions, e.g.
void f(int& r);
cout << i;
void f(int& r)
r = 2*r;
This program prints "6" (2*r doubles the value of the variable referenced by r, namely, i).
We could do the same in C by declaring f() as void f(int *r), in which case r is a pointer to an int, then calling f() with argument &i (address-of i), and using de-referencing of r within f(), but clearly C++ provides a more elegant way of passing values to functions (by reference) and returning (perhaps multiple) values from functions (without use of a return statement).
An experienced programmer's view of C, C++ and Java C/C++ Programming Hermetic Systems Home Page