Page 80 - Programming Microcontrollers in C
P. 80
Chapter 2
Advanced C Topics
Pointers
The use of pointers sets the C language apart from most other
high-level languages. Pointers, under the name of indirect addressing,
are commonly used in assembly language. Most high-level languages
completely ignore this powerful programming technique. In C, point
ers are simply variables that are the addresses of other variables. These
variables can be assigned, be used as operands, and treated much the
same as regular variables. Their use, however, can simplify greatly
many of the truly difficult problems that arise in day-to-day program
ming. Let’s discuss pointers with the view that they offer us a powerful
new tool to make our programming jobs easier.
How To Use Pointers
A pointer to a variable is the address of a variable. Thus far in our
discussion of variables, the standard variable types were found to
occupy 8-, 16-, or 32 bits depending on the type. Pointers are not
types in the sense of variables. Pointers might occupy some number
of bits, and their size is implementation dependent. In fact, there are
cases of pointers to like types having different sizes in the same pro
gram depending on context.
If the variable px is a pointer to the type int and x is an int, px
can be assigned the address of x by
px = &x;
The ampersand (&) notifies the compiler to use the address of x
rather than the value of x in the above assignment. The reverse op
eration to that above is
65