In
computer programming
Computer programming is the process of performing a particular computation (or more generally, accomplishing a specific computing result), usually by designing and building an executable computer program. Programming involves tasks such as ana ...
, the dereference operator or indirection operator, sometimes denoted by "
*
" (i.e. an
asterisk
The asterisk ( ), from Late Latin , from Ancient Greek , ''asteriskos'', "little star", is a typographical symbol. It is so called because it resembles a conventional image of a heraldic star.
Computer scientists and mathematicians often voc ...
), is a
unary operator
In mathematics, an unary operation is an operation with only one operand, i.e. a single input. This is in contrast to binary operations, which use two operands. An example is any function , where is a set. The function is a unary operation on ...
(i.e. one with a single operand) 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
An address is a collection of information, presented in a mostly fixed format, used to give the location of a building, apartment, or other structure or a plot of land, generally using political boundaries and street names as references, along ...
. This is called "dereferencing" the pointer. For example, the C code
int x;
int *p; // * is used in the declaration:
// p is a pointer to an integer, since (after dereferencing),
// *p is an integer
x = 0;
// now x 0
p = &x; // & takes the address of x
// now *p 0, since p &x and therefore *p x
*p = 1; // equivalent to x = 1, since p &x
// now *p 1 and x 1
assigned 1 to variable
x
by using the dereference operator and a pointer to the variable
x
.
Composition
The
unary * operator, as defined in
C and
C++
C++ (pronounced "C plus plus") is a high-level general-purpose programming language created by Danish computer scientist Bjarne Stroustrup as an extension of the C programming language, or "C with Classes". The language has expanded significan ...
, 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 of multiple pointer
indirection
In computer programming, indirection (also called dereferencing) is the ability to reference something using a name, reference, or container instead of the value itself. The most common form of indirection is the act of manipulating a value throug ...
is the
argv
In computer programming, an entry point is the place in a program where the execution of a program begins, and where the program has access to command line arguments.
To start a program's execution, the loader or operating system passes contro ...
argument to the
main function in C (and C++), which is given in the prototype as
char **argv
. The name of the invoked program executable, as well as all command line arguments that followed, are stored as independent character strings. An array of pointers to
char
contains pointers to the first character of each of these strings, and this array of pointers is passed to the
main
function as the
argv
argument. The passed array itself "decays" to a pointer, thus
argv
is actually a pointer to a pointer to
char
, even though it stands for an array of pointers to
char
(similarly, the pointers in the array, while each formally pointing to a single
char
, actually point to what are strings of characters). The accompanying
main
argument,
argc
, provides the size of the array (i.e. the number of strings pointed to by the elements of the array), as the size of an (outmost) array is otherwise lost when it is passed to a function and converted to a pointer. Thus,
argv
is a pointer to the 0th element of an array of pointers to
char
,
*argv
, which in turn is a pointer to
**argv
, a character (precisely, the 0th character of the first argument string, which by convention is the name of the program).
Other syntax
In
BCPL
BCPL ("Basic Combined Programming Language") is a procedural, imperative, and structured programming language. Originally intended for writing compilers for other languages, BCPL is no longer in common use. However, its influence is still ...
, an ancestor of C, the equivalent operator was represented using an
exclamation mark
The exclamation mark, , or exclamation point (American English), is a punctuation mark usually used after an interjection or exclamation to indicate strong feelings or to show emphasis. The exclamation mark often marks the end of a sentence, f ...
.
In C, the address of a structure (or union)
s
is denoted by
&s
. The address of operator
&
is the right inverse of the dereferencing operator
*
, so
*&s
is equivalent to
s
. (However, note that
&*s
only is equivalent to
s
if
s
is a pointer variable; else the expression does not make sense.) The address of a structure (or union)
s
may be assigned to a pointer
p
:
p = &s; // the address of s has been assigned to p; p &s;
// *p is equivalent to s
The value of a member
a
of a structure
s
is denoted by
s.a
. Given a pointer
p
to
s
(i.e.
p &s
),
s.a
is equivalently to
(*p).a
, and also to the shorthand
p->a
which is
syntactic sugar
In computer science, syntactic sugar is syntax within a programming language that is designed to make things easier to read or to express. It makes the language "sweeter" for human use: things can be expressed more clearly, more concisely, or in an ...
for
accessing members of a structure (or union) through a pointer:
p = &s; // the address of s has been assigned to p; p &s;
// s.a is equivalent to (*p).a
// s.a is equivalent to p->a
// (*p).a is equivalent to p->a
The
->
operator 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
A shell script is a computer program designed to be run by a Unix shell, a command-line interpreter. The various dialects of shell scripts are considered to be scripting languages. Typical operations performed by shell scripts include file manip ...
and in utilities such as
Makefile
In software development, Make is a build automation tool that automatically builds executable programs and libraries from source code by reading files called ''Makefiles'' which specify how to derive the target program. Though integrated develo ...
s, 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
Pascal
Pascal, Pascal's or PASCAL may refer to:
People and fictional characters
* Pascal (given name), including a list of people with the name
* Pascal (surname), including a list of people and fictional characters with the name
** Blaise Pascal, Fren ...
, the dereference operator
^ works to both define a pointer and to dereference it. As the following example shows:
Type
ComplexP = ^TComplex; (* ComplexP is a pointer type *)
TComplex = record (* TComplex is a record type *)
Re,
Im: Integer;
VAR
Complex1, (* define two pointers *)
Complex2: ComplexP;
Complex : TComplex; (* define a record *)
begin
Complex.Re := 3.14159267;
Complex.Im := 1.5;
New(Complex1);
Complex1^.Re := Complex.Re;
Complex1^.Im := 3.5;
New(Complex2);
Complex2^ := Complex;
END.
Int the above example
* On line 2, the dereference operator ^ is used to define a pointer type .
* On lines 12 and 13, values are being assigned to the and fields of the record.
* On line 14, space is allocated for a record pointed to by ( is Pascal's equivalent of C's function.)
* On Line 15, the dereference operator is used to copy the value in the field of record to the field of the record pointed to by .
* On line 16, the dereference operator is used to assign a value to the field of the record pointed to by .
* On line 17, space is allocated for a record pointed to by .
* On Line 18, the entire record is copied to the record pointed to by .
In various languages, prefixes are used in identifiers, known as
sigil
A sigil () is a type of symbol used in magic. The term has usually referred to a pictorial signature of a deity or spirit. In modern usage, especially in the context of chaos magic, sigil refers to a symbolic representation of the practitioner ...
s. 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
In computer programming, a variable is an abstract storage location paired with an associated symbolic name, which contains some known or unknown quantity of information referred to as a ''value''; or in simpler terms, a variable is a named cont ...
called
foo
. In PHP, FOO is a constant (user defined or built-in), $FOO is a variable named FOO and $$FOO is a variable, whose name is stored in variable named FOO.
See also
*
Segmentation fault
In computing, a segmentation fault (often shortened to segfault) or access violation is a fault, or failure condition, raised by hardware with memory protection, notifying an operating system (OS) the software has attempted to access a restricte ...
{{DEFAULTSORT:Dereference Operator
Operators (programming)
Unary operations