Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.


9.3.4 Point to other elements

We saw in the previous section how to increment a pointer so that it points to the next element in the array. Decrementing a pointer p (i.e., making it point to the previous element) is done in a similar way, using the p- statement. Actually, p++ and p- are really expressions which denote pointer values. p++ denotes the address stored in p before it is incremented, and p- denotes the address after p is decremented. Therefore,

  addr:=p
  p++

does the same as

  addr:=p++

And

  p--
  addr:=p

does the same as

  addr:=p--

The reason why ++ and - should be used to increment and decrement a pointer is that values from different types occupy different numbers of memory locations. In fact, a single memory location is a byte, and this is eight bits. Therefore, CHAR values occupy a single byte, whereas LONG values take up four bytes (32 bits). If p were a pointer to CHAR and it was pointing to an array (of CHAR) the p+1 memory location would contain the second element of the array (and p+2 the third, etc.). But if p were a pointer to an array of LONG the second element in the array would be at p+4 (and the third at p+8). The locations p, p+1, p+2 and p+3 all make up the LONG value at address p. Having to remember things like this is a pain, and it's a lot less readable than using ++ or -. However, you must remember to declare your pointer with the correct type in order for ++ and - to work correctly.


Go to the Next or Previous section, the Detailed Contents, or the Amiga E Encyclopedia.