# Multidimensional Pointer Arithmetic in C/C++

Here we discuss Multidimensional Pointer Arithmetic Arrays and pointers have comparable semantics in C/C++, with the exception of type information.

Consider a 3D array as an example.

``int buffer;``

An element at location  can be accessed as “buffer” or *( *( *(buffer + 2) + 1) + 2).

Observe the following declaration

``T *p; // p is a pointer to an object of type T``

The phrase *p is of type T when a pointer p points to an object of type T. Buffer, for example, is a five-dimensional array of type array. “Array of arrays (i.e. two-dimensional array)” is the type of the expression *buffer.

Translating the expression ((*(buffer + 2) + 1) + 2) step by step, based on the above principle, makes it more understandable.

1. buffer – An array of 5 two dimensional arrays, i.e. its type is “array of 5 two dimensional arrays”.
2. buffer + 2 – displacement for 3rd element in the array of 5 two dimensional arrays.
3. *(buffer + 2) – dereferencing, i.e. its type is now two dimensional array.
4. *(buffer + 2) + 1 – displacement to access 2nd element in the array of 7 one dimensional arrays.
5. *( *(buffer + 2) + 1) – dereferencing (accessing), now the type of expression “*( *(buffer + 2) + 1)” is an array of integers.
6. *( *(buffer + 2) + 1) + 2 – displacement to get element at 3rd position in the single dimension array of integers.
7. *( *( *(buffer + 2) + 1) + 2) – accessing the element at 3rd position (the overall expression type is int now).

To access an array element, the compiler calculates an “offset.” The “offset” is calculated using the array’s dimensions. offset = 2 * (7 * 6) + 1 * (6) + 2 in the example above. Dimensions are those in blue; note that the upper dimension is not considered in the offset calculation. The compiler is aware of the array dimensions during compilation. We can access the element using offset as shown below,

``````element_data = *( (int *)buffer + offset );
``````

At compilation time, declaring array dimensions is not always possible. We need to interpret a buffer as a multidimensional array object on occasion. Array subscript rules cannot be employed, for example, while processing a 3D image whose dimensions are decided at run-time. It’s because there aren’t any set dimensions during the compilation process. Consider the following illustration:

``int *base;``

Where base points to a big image buffer that depicts a 3D image with dimensions of l x b x h and variables l, b, and h. If we wish to access an element at position (2, 3, 4), we must calculate the element’s offset as follows:

The element is positioned at base + offset and offset = 2 * (b x h) + 3 * (h) + 4.

Further generalizing, we can access an element at any arbitrary place (a, b, c) in an array of size [l x b x h] dimensions in the following method, given the start address (say base).

data = *(base + a * (b x h) + b * (h) + c); // Note that we haven’t used the higher dimension l.

Any number of dimensions can be used to apply the same principle. The higher dimension isn’t required to calculate the offset of any element in the multidimensional array. When we send multidimensional arrays to functions, we omit the higher dimension for this reason. The higher dimension is only required when the programmer is iterating over a small number of higher dimension components.

A C/C++ puzzle, predict the output of the following program:

```int main()
{
char arr;
char (*p) = &arr;
/* Hint: &arr - is of type const pointer to an array of
5 two dimensional arrays of size  */
printf("%d\n", (&arr + 1) - &arr);
printf("%d\n", (char *)(&arr + 1) - (char *)&arr);
printf("%d\n", (unsigned)(arr + 1) - (unsigned)arr);
printf("%d\n", (unsigned)(p + 1) - (unsigned)p);
return 0;
}```

Output:

``````Output:
1
210
42
210`````` 