Reference and Dereference Operators

reference and dereference operators

Pointers basics

A pointer differs in the way that a pointer is a variable that points to another variable. A pointer holds the memory address of that variable. That variable contains a value. Pointers are also called address variables because they contain the addresses of other variables.

Initialize a pointer

To declare a pointer you have to put an * in front of its name. A pointer can be typed or un-typed. (A typed pointer points to a particular variable type such as an integer. An un-typed pointer points to any data type).


int x; int *ptr_p; x = 5; ptr_p = &x;

Reference and dereference operators

In the example above we used ampersand sign (&). This sign is called the reference operator. If the reference operator is used we will get the “address of” a variable.

In the example above we said: ptr_p = &x;. In words: store the address of the variable x in the pointer ptr_p.

We also used the asterisk sign (*) in the cout statement. This sign is called the dereference operator. If the dereference operator is used you will get the “value pointed by” a pointer.

*ptr_p;. In words: the value pointed by ptr_p. (It will print the contents of integer x.)

Note: The asterisk (*) sign in the declaration of the pointer does not mean “value pointed by”(e.g. int *ptr_p is a declaration ), it only means that it is a pointer . It should not be confused with the dereference operator.

They are simply two different things represented with the same sign. So we can say:

& is the reference operator and can be read as “address of”.

* is the dereference operator and can be read as “value pointed by”.