Node:Pointer operators, Next:Pointer types, Previous:Pointers, Up:Pointers
Pointer operators
To create a pointer to a variable, we use the *
and &
operators. (In context, these have nothing to do with multiplication or
logical AND. For example, the following code declares a variable called
total_cost
and a pointer to it called total_cost_ptr
.
float total_cost; float *total_cost_ptr; total_cost_ptr = &total_cost;
The *
symbol in the declaration of total_cost_ptr
is the
way to declare that variable to be a pointer in C. (The _ptr
at the
end of the variable name, on the other hand, is just a way of reminding
humans that the variable is a pointer.)
When you read C code to yourself, it is often useful to be able to
pronounce C's operators aloud; you will find it can help you make sense
of a difficult piece of code. For example, you can pronounce the above
statement float *total_cost_ptr
as "Declare a float pointer
called total_cost_ptr
", and you can pronounce the statement
total_cost_ptr = &total_cost;
as "Let total_cost_ptr
take
as its value the address of the variable total_cost
".
Here are some suggestions for pronouncing the *
and &
operators, which are always written in front of a variable:
*
- "The contents of the address held in variable" or
"the contents of the location pointed to by variable".
&
- "The address of variable" or "the address at which the variable variable is stored".
For instance:
&fred
- "The address of
fred
" or "the address at which the variablefred
is stored". *fred_ptr
- "The contents of the address held in
fred_ptr
" or "the contents of the location pointed to byfred_ptr
".
The following examples show some common ways in which you might use the
*
and &
operators:
int some_var; /* 1 */ "Declare an integer variable calledsome_var
." int *ptr_to_some_var; /* 2 */ "Declare an integer pointer calledptr_to_some_var
." (The*
in front ofptr_to_some_var
is the way C declaresptr_to_some_var
as a pointer to an integer, rather than just an integer.) some_var = 42; /* 3 */ "Letsome_var
take the value 42." ptr_to_some_var = &some_var; /* 4 */ "Letptr_to_some_var
take the address of the variablesome_var
as its value." (Notice that only now doesptr_to_some_var
become a pointer to the particular variablesome_var
-- before this, it was merely a pointer that could point to any integer variable.) printf ("%d\n\n", *ptr_to_some_var); /* 5 */ "Print out the contents of the location pointed to byptr_to_some_var
." (In other words, print outsome_var
itself. This will print just 42. Accessing what a pointer points to in this way is called dereferencing the pointer, because the pointer is considered to be referencing the variable.) *ptr_to_some_var = 56; /* 6 */ "Let the contents of the location pointed to byptr_to_some_var
equal 56." (In the context of the other statements, this is the same as the more direct statementsome_var = 56;
.)
A subtle point: don't confuse the usage of asterisks in code like examples 2 and 6 above. Using an asterisk in a declaration, as in example 2, declares the variable to be a pointer, while using it on the left-hand side of an assignment, as in example 6, dereferences a variable that is already a pointer, enabling you to access the variable to which the pointer is pointing.