User manual

Table Of Contents
208
mikoC PRO for PIC32
MikroElektronika
The following statements are true:
&a[i] = a + i
a[i] = *(a + i)
According to these guidelines, it can be written:
pa = &a[4]; // pa points to a[4]
x = *(pa + 3); // x = a[7]
/* .. but: */
y = *pa + 3; // y = a[4] + 3
Also the care should be taken when using operator precedence:
*pa++; // Equal to *(pa++), increments the pointer
(*pa)++; // Increments the pointed object!
The 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
The simple assignment operator (=) can be used 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 the void type).
Assigning the integer constant 0 to a pointer assigns a null pointer value to it.
Two pointers pointing to the same array may be compared by using relational operators ==, !=, <, <=, >, and
>=. Results of these operations are the same as if they were used on subscript values of array elements in question:
int *pa = &a[4], *pb = &a[2];
if (pa == pb) {... /* won’t be executed as 4 is not equal to 2 */ }
if (pa > pb) {... /* will be executed as 4 is greater than 2 */ }
You can also compare pointers to zero value – testing in that way if the pointer actually points to anything. All pointers
can be successfully tested for equality or inequality to null:
if (pa == 0) { ... }
if (pb != 0) { ... }
Note: Comparing pointers pointing to different objects/arrays can be performed at programmer’s own responsibility
— a precise overview of data’s physical storage is required