Dereference operator
| This article does not cite any references or sources. (March 2007) |
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 an l-value equivalent to the value at the pointer address. This is called "dereferencing" the pointer. For example, the C code
int x = 0; int *pointer_to_x = &x; // * is used to do declaration (*pointer_to_x) = 1; // * is a dereference operator //x is now equal to 1
assigned 1 to variable x by using the dereference operator and a pointer to the variable x.
Java
Many other operators exist to dereference pointers, and this is of significant importance especially in object-oriented languages. In Java for example there is a binary operator occasionally named "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 pointer X, yielding an object, and then accesses the member Y from that object. For example, the Java code
int[] 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 pointer a. The dot operator is then used to dereference the pointer a and access the length member of the array object, storing the value in variable c.
Composition
The unary * operator, as defined in C and C++, can be used in compositions in cases of multiple indirection, 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.
A basic example is in the argv argument to the main function in C (and C++), which is given in the prototype as char **argv – this is because the variable argv itself is a pointer to an array of strings (an array of arrays), so *argv is a pointer to the 0th string (by convention the name of the program), and **argv is the 0th character of the 0th string.
Other syntax
In BCPL, an ancestor of C, the equivalent operator was represented using an exclamation mark.
In C, there is syntactic sugar for accessing members of a struct or union, given a pointer to such. Given a pointer p to a structure s so:
*p = s
the usual way to access a member a is as s.a which, given the pointer, is expressed as (*p).a or can instead be accessed by the shorthand:
p->a
This can be chained; for example, in a linked list, one may refer to n->next->next for the second following node (assuming that n->next is not null).
In Unix shell scripting and in utilities such as Makefiles, the dollar sign "$" is the dereference operator, used to translate the name of a variable into its contents, and is notably absent when assigning to a variable.
In various languages, prefixes are used in identifiers, known as sigils. These are not unary operators – syntactically they are lexically part of the identifier, and have different semantics, such as indicating the data type of the identifier – but are syntactically similar to the dereference operator and can be confused with it. For example, in a shell script $FOO is the dereference operator $ applied to the variable FOO, while in Perl $foo is a scalar variable called foo.
See also
| This computer programming-related article is a stub. You can help Wikipedia by expanding it. |
Read in another language
This page is available in 1 language
