Dereference operator

Updated: 04/26/2017 by Computer Hope

In computer programming, a dereference operator, also known as an indirection operator, operates on a pointer variable, and returns the location-value, or l-value that it points to in memory. In the C programming language, the deference operator is denoted with an asterisk (*).

For example, in C, we can declare a variable x, that holds an integer value, and a variable p, which holds a pointer to an integer value in memory:

int x; int *p;

Here, the asterisk tells the compiler, "p is not an integer, but rather a pointer to a location in memory which holds an integer." Here, it is not a dereference, but part of a pointer declaration.

Now we can set p to the location allocated for the value of x using the & operator, which means "address of."

p = &x;

This action tells the compiler, "The address in memory that p points to is the address that you allocated for the integer x."

To illustrate: if we set the value of x to 1 using the conventional method, and print the value, the output will be 1.

x = 1; printf("%d", x);

However, we can also change the value of x by referencing p. We do this with an asterisk:

*p = 2; printf("%d", x);

And the output changes to 2.

In other words, after p has been declared as a pointer of the same type as x and then set to point to x's value, we can use x and *p interchangeably. Since they both refer to the same thing, changing the value of one will change the value of the other.

Pointer, Programming terms, Variable