Complex pointer manipulation and address calculations

When calculating the position of an array element using a pointer and an offset, it is very important to remember that indexes into arrays are automatically scaled by the size of an array element appropriote to the array being indexed AND that offsets from pointers are not. Offsets from pointers must therefore be scaled by the user.
e.g.

If we have an array of 16 bit integers called FRED starting at location 100 in data memory, then element 0 will be at address 100, element 1 will be at 102, element 2 will be at 104, element 3 will be at 106. In general element N will be at 100 + (N * 2). The index for this array is scaled by 2. The index for all arrays of 16 bit integers is scaled by 2.

If we have an array of 32 bit integers called BERT starting at location 100 in data memory, then element 0 will be at address 100, element 1 will be at 104, element 2 will be at 108, element 3 will be at 112. In general element N will be at 100 + (N * 4). The index for this array is scaled by 4. The index for all arrays of 32 bit integers is scaled by 4.

Calculating the address of an element within an integer array using a pointer and an offset.
	int	arr[10]

	int	*ptr, *ptr2

	ptr = &arr

	ptr2 = (ptr + j*2)
NOTE: in XCSB arr is equivalent to arr[0] and *(arr + j) is equivalent to *(arr[0] + j)