Mikroc – ABL electronic PIC Microcontrollers PIC16 User Manual

Page 79

Advertising
background image

According to these guidelines, we can write:

pa = &a[4];

// pa points to a[4]

x = *(pa + 3);

// x = a[7]

y = *pa + 3;

// y = a[4] + 3

Also, you need to be careful with operator precedence:

*pa++;

// is equal to *(pa++), increments the pointer!

(*pa)++;

// increments the pointed object!

Following examples are also valid, but better avoid this syntax as it can make the
code really illegible:

(a + 1)[i] = 3;

// same as: *((a + 1) + i) = 3, i.e. a[i + 1] = 3

(i + 2)[a] = 0;

// same as: *((i + 2) + a) = 0, i.e. a[i + 2] = 0

Assignment and Comparison

You can use a simple assignment operator (

=

) to assign value of one pointer to

another if they are of the same type. If they are of different types, you must use a
typecast operator. Explicit type conversion is not necessary if one of the pointers is
generic (of

void

type).

Assigning the integer constant 0 to a pointer assigns a null pointer value to it. The
mnemonic

NULL

(defined in the standard library header files, such as

stdio.h

)

can be used for legibility.

Two pointers pointing into the same array may be compared by using relational
operators

==

,

!=

,

<

,

<=

,

>

, and

>=

. Results of these operations are same as if they

were used on subscript values of array elements in question:

int

*pa = &a[4], *pb = &a[2];

if

(pa > pb) { ...

// this will be executed as 4 is greater than 2

}

MikroElektronika: Development tools - Books - Compilers

71

page

mikroC - C Compiler for Microchip PIC microcontrollers

mikroC

making it simple...

Advertising
This manual is related to the following products: