Datasheet

POINTERS
Pointers are special objects for holding (or “pointing to”) memory addresses. In the
mikroC PRO for AVR, 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 inte-
gers, they have their own rules and restrictions for declarations, assignments, con-
versions, 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 identifier.
A type at the beginning of declaration specifies the type of a pointed object. A point-
er 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 AVR.
If type is any predefined or user-defined 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 point-
er
*p is not initialized (i.e. assigned a value), so it cannot be used yet.
Note: In case of multiple pointer declarations, each identifier requires an indirect
operator. For example:
158
MIKROELEKTRONIKA - SOFTWARE AND HARDWARE SOLUTIONS FOR EMBEDDED WORLD
Language Reference
mikroC PRO for AVR
CHAPTER 5