- Dereference operator
-
The dereference operator or indirection operator, denoted by "
*
" (i.e. an asterisk), is a unary operator found in C-like languages that include pointer variables. It operates on a pointer variable, and returns anl-value
equivalent to the value at the pointer address. This is called "dereferencing" the pointer. For example, the C codeint x = 0; int *pointer_to_x = &x; (*pointer_to_x) += 1; //x is now equal to 1
increments the variable
x
by using the indirection operator and a pointer to the variablex
.In BCPL, an ancestor of C, the equivalent operator was represented using an exclamation mark.
Many other operators exist to dereference pointers, and this is of significant importance especially in Object Oriented languages. In Java for example there is the binary operator, "dot," which is placed by infix notation between an object reference on the left and a member of that object's class on the right. In the form
X.Y
the dot operator dereferences the pointerX
, yielding an object, and then accesses the memberY
from that object. For example, the Java codeint[] a = new int[]{1, 2, 3}; int c = a.length;
first creates an array of
int
primitives, and stores a reference to that array in pointera
. The dot operator is then used to dereference the pointera
and access thelength
member of the array object, storing the value in variablec
.The unary * operator, as defined in C and C++, can be used in compositions where multiple acts of dereferencing are required. Pointers can of course reference other pointers, and in such cases, multiple applications of the dereference operator are needed. Similarly, the Java dot operator can be used in compositions forming quite sophisticated statements that require substantial dereferencing of pointers behind the scenes during evaluation.
See also
Categories:- Operators (programming)
- Computer programming stubs
Wikimedia Foundation. 2010.