Dereference operator

Updated: 12/31/2022 by Computer Hope

In computer programming, a dereference operator, also known as an indirection operator, operates on a pointer variable. It returns the location value, or l-value in memory pointed to by the variable's value. 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 that 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 is 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 changes the value of the other.

Pointer, Programming terms, Variable