The first line declare a normal variable, num, which is initialized to contain the data 7. Next, a pointer variable,
numPtr, is declared, which is initialized to point to the address of num. The next four lines use the printf( )
function, which causes some data to be printed to some display terminal connected to the serial port. The first
such line displays the contents of the num variable, which is in this case the value 7. The next displays the
contents of the numPtr pointer, which is really some weird-looking number that is the address of the num variable.
The third such line also displays the addresss of the num variable because the address operator is used to obtain
num's address. The last line displays the actual data to which the numPtr pointer is pointing, which is 7. The *
symbol is called the indirection operator, and when used with a pointer, indirectly obtains the data whose address
is pointed to by the pointer. Therefore, the output display on the terminal would show:
7
13452 (or some other weird-looking number)
13452 (or some other weird-looking number)
7
A Pointer's Memory Type
Recall that pointers are also variables, so the question arises where they should be stored. When declaring
pointers, we can specify different types of memory areas that these pointers should be in, for example:
int * xdata numPtr = & num;
This is the same as our previous pointer examples. We declare a pointer numPtr, which points to data of type int
stored in the num variable. The difference here is the use of the memory type specifier xdata after the *. This is
specifies that pointer numPtr should reside in external data memory (xdata), and we say that the pointer's memory
type is xdata.
Typed Pointers
We can go even further when declaring pointers. Consider the example:
int data * xdata numPtr = #
The above statement declares the same pointer numPtr to reside in external data memory (xdata), and this pointer
points to data of type int that is itself stored in the variable num in internal data memory (data). The memory type
specifier, data, before the * specifies the data memory type while the memory type specifier, xdata, after the *
specifies the pointer memory type.
Pointer declarations where the data memory types are explicitly specified are called typed pointers. Typed
pointers have the property that you specify in your code where the data pointed by pointers should reside. The
size of typed pointers depends on the data memory type and could be one or two bytes.
Untyped Pointers
When we do not explicitly state the data memory type when declaring pointers, we get untyped pointers, which
are generic pointers that can point to data residing in any type of memory. Untyped pointers have the advantage
that they can be used to point to any data independent of the type of memory in which the data is stored. All
untyped pointers consist of 3 bytes, and are hence larger than typed pointers. Untyped pointers are also generally
slower because the data memory type is not determined or known until the complied program is run at runtime.
The first byte of untyped pointers refers to the data memory type, which is simply a number according to the
following table. The second and third bytes are,respectively,the higher-order and lower-order bytes of the address
being pointed to.
An untyped pointer is declared just like normal C, where:
int * xdata numPtr = #
does not explicitly specify the memory type of the data pointed to by the pointer. In this case, we are using
untyped pointers.
246