User manual

206
mikoC PRO for dsPIC
MikroElektronika
Pointers
Pointers are special objects for holding (or “pointing to”) memory addresses. In the mikroC PRO for dsPIC30/33 and
PIC24, address of an object in memory can be obtained by means of an unary operator &. To reach the pointed object,
we use an indirection operator (*) on a pointer.
A pointer of type “pointer to object of type” holds the address of (that is, points to) an object of type. Since pointers are
objects, you can have a pointer pointing to a pointer (and so on). Other objects commonly pointed to include arrays,
structures, and unions.
A pointer to a function is best thought of as an address, usually in a code segment, where that function’s executable
code is stored; that is, the address to which control is transferred when that function is called.
Although pointers contain numbers with most of the characteristics of unsigned integers, they have their own rules and
restrictions for declarations, assignments, conversions, and arithmetic. The examples in the next few sections illustrate
these rules and restrictions.
Pointer Declarations
Pointers are declared the same as any other variable, but with * ahead of identier. A type at the beginning of declaration
species the type of a pointed object. A pointer must be declared as pointing to some particular type, even if that type
is void, which really means a pointer to anything. Pointers to void are often called generic pointers, and are treated
as pointers to char in the mikroC PRO for dsPIC30/33 and PIC24.
If type is any predened or user-dened type, including void, the declaration
type *p; /* Uninitialized pointer */
declares p to be of type “pointer to type”. All scoping, duration, and visibility rules are applied to the p object just
declared. You can view the declaration in this way: if *p is an object of type, then p has to be a pointer to such object
(object of type).
Note: You must initialize pointers before using them! Our previously declared pointer *p is not initialized (i.e.
assigned a value), so it cannot be used yet.
In case of multiple pointer declarations, each identier requires an indirect operator. For example:
int *pa, *pb, *pc;
// is same as :
int *pa;
int *pb;
int *pc;
Once declared, though, a pointer can usually be reassigned so that it points to an object of another type. The mikroC
PRO for dsPIC30/33 and PIC24 lets you reassign pointers without typecasting, but the compiler will warn you unless
the pointer was originally declared to be pointing to void. You can assign the void* pointer to the non-void* pointer
– refer to void for details.