Search Menu


Problems and Solutions 2

Problem : What dimensions do you need to know to determine the value of arr[3][5][7] using only pointer arithmetic and the dereference operator (*)?

You do not need to know the first dimension, but you do need to know the second and the third dimension. In general you need to know all but the first dimension.

Problem : Assuming that the second dimension were SECOND and the third dimension were THIRD, calculate the value of arr[3][5][7] from the pointer arr using pointer arithmetic and the dereference operator.

The equivalent is:

*(arr + 3 * SECOND * THIRD + 5 * THIRD + 7)
In general you multiply each index by the size of the greater dimensions. So the first index, 3, is multiplied by the second and third dimensions and the second index, 5, is multiplied by the third dimension.

Problem : As an exercise, write some code that will print out all of the integers in the integer array without ever using the square brackets to index into it. There are n integers in the array arr.

int i;
for(i = 0; i < n; i++)
	printf("%i\n", *(arr + i);