Mikroelektronika MIKROE-738 Datenbogen
204
mikoC PRO for PIC32
MikroElektronika
Null Pointers
A null pointer value is an address that is guaranteed to be different from any valid pointer in use in a program. Assigning
the integer constant 0 to a pointer assigns a null pointer value to it.
For example:
int *pn = 0; /* Here’s one null pointer */
/* We can test the pointer like this: */
if ( pn == 0 ) { ... }
The pointer type “pointer to void” must not be confused with the null pointer. The declaration
void *vp;
declares that
vp
is a generic pointer capable of being assigned to by any “pointer to type” value, including null, without
complaint.
Assignments without proper casting between a “pointer to
type1
” and a “pointer to
type2
”, where
type1
and
type2
are different types, can invoke a compiler warning or error. If
type1
is a function and
type2
isn’t (or vice versa),
pointer assignments are illegal. If
type1
is a pointer to
void
, no cast is needed. If
type2
is a pointer to
void
, no
cast is needed.
Function Pointers
Function Pointers are pointers, i.e. variables, which point to the address of a function.
// Define a function pointer
int (*pt2Function) (float, char, char);
Note: Thus functions and function pointers with different calling convention (argument order, arguments type or return
type is different) are incompatible with each other.
Assign an address to a Function Pointer
It’s quite easy to assign the address of a function to a function pointer. Simply take the name of a suitable and known
function. Using the address operator & infront of the function’s name is optional.
//Assign an address to the function pointer
int DoIt (float a, char b, char c){ return a+b+c; }
pt2Function = &DoIt; // assignment