Multidimensional Pointer Arithmetic in C/C++
Last Updated :
28 Sep, 2018
In C/C++, arrays and pointers have similar semantics, except on type information.
As an example, given a 3D array
int buffer[5][7][6];
An element at location [2][1][2] can be accessed as "
buffer[2][1][2]" or
*( *( *(buffer + 2) + 1) + 2).
Observe the following declaration
T *p; // p is a pointer to an object of type T
When a pointer
p is pointing to an object of type T, the expression
*p is of type T. For example
buffer is of type array of 5 two dimensional arrays. The type of the expression
*buffer is "array of arrays (i.e. two dimensional array)".
Based on the above concept translating the expression
*( *( *(buffer + 2) + 1) + 2) step-by-step makes it more clear.
- buffer - An array of 5 two dimensional arrays, i.e. its type is "array of 5 two dimensional arrays".
- buffer + 2 - displacement for 3rd element in the array of 5 two dimensional arrays.
- *(buffer + 2) - dereferencing, i.e. its type is now two dimensional array.
- *(buffer + 2) + 1 - displacement to access 2nd element in the array of 7 one dimensional arrays.
- *( *(buffer + 2) + 1) - dereferencing (accessing), now the type of expression "*( *(buffer + 2) + 1)" is an array of integers.
- *( *(buffer + 2) + 1) + 2 - displacement to get element at 3rd position in the single dimension array of integers.
- *( *( *(buffer + 2) + 1) + 2) - accessing the element at 3rd position (the overall expression type is int now).
The compiler calculates an "offset" to access array element. The "offset" is calculated based on dimensions of the array. In the above case,
offset = 2 * (7 * 6) + 1 * (6) + 2. Those in
blue colour are dimensions,
note that the higher dimension is not used in offset calculation. During compile time the compiler is aware of dimensions of array. Using offset we can access the element as shown below,
element_data = *( (int *)buffer + offset );
It is not always possible to declare dimensions of array at compile time. Sometimes we need to interpret a buffer as multidimensional array object. For instance, when we are processing 3D image whose dimensions are determined at run-time, usual array subscript rules can't be used. It is due to lack of fixed dimensions during compile time. Consider the following example,
int *base;
Where
base is pointing large image buffer that represents 3D image of dimension
l x b x h where l, b and h are variables. If we want to access an element at location (2, 3, 4) we need to calculate offset of the element as
offset = 2 * (b x h) + 3 * (h) + 4 and the element located at
base + offset.
Generalizing further, given start address (say
base) of an array of size [
l x b x h] dimensions, we can access the element at an arbitrary location (
a, b, c) in the following way,
data = *(base + a * (b x h) + b * (h) + c); // Note that we haven't used the higher dimension
l.
The same concept can be applied to any number of dimensions. We don't need the higher dimension to calculate offset of any element in the multidimensional array.
It is the reason behind omitting the higher dimension when we pass multidimensional arrays to functions. The higher dimension is needed only when the programmer iterating over limited number of elements of higher dimension.
A C/C++ puzzle, predict the output of following program
c
int main()
{
char arr[5][7][6];
char (*p)[5][7][6] = &arr;
/* Hint: &arr - is of type const pointer to an array of
5 two dimensional arrays of size [7][6] */
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:
1
210
42
210
Thanks to
student for pointing an error.
---
Venki.
Similar Reads
C++ Pointer Arithmetic In C++, pointer arithmetic means performing arithmetic operations on pointers. It refers to the operations that are valid to perform on pointers. Following are the arithmetic operations valid on pointers in C++:Table of ContentIncrementing and Decrementing Pointer in C++Addition of Constant to Point
7 min read
Character Arithmetic in C++ Character arithmetic in C++ involves performing mathematical operations on characters. In C++, characters are represented using the char data type, which is essentially an integer type that stores ASCII values. In this article, we will discuss character arithmetic and how to perform character arithm
2 min read
Application of Pointer in C++ In C++, pointer is a variable that stores the memory address as its value. It is a powerful feature that forms the backbone of lower-level memory manipulation in C++. It is used in a variety of applications. Some of the common applications of pointers are discussed in this article.1. Pass Arguments
4 min read
Applications of Pointers in C Pointers in C are variables that are used to store the memory address of another variable. Pointers allow us to efficiently manage the memory and hence optimize our program. In this article, we will discuss some of the major applications of pointers in C. Prerequisite: Pointers in C. C Pointers Appl
4 min read
Sum of array using pointer arithmetic Given an array, write a program to find the sum of array using pointers arithmetic. In this program we make use of * operator . The * (asterisk) operator denotes the value of variable. The * operator at the time of declaration denotes that this is a pointer, otherwise it denotes the value of the mem
2 min read
C++ Arithmetic Operators Arithmetic Operators in C++ are used to perform arithmetic or mathematical operations on the operands (generally numeric values). An operand can be a variable or a value. For example, â+â is used for addition, '-' is used for subtraction, '*' is used for multiplication, etc. Let's take a look at an
4 min read