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
int main()
{
char arr[5][7][6];
char (*p)[5][7][6] = &arr;
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
Pointer Arithmetics in C with Examples
Pointer Arithmetic is the set of valid arithmetic operations that can be performed on pointers. The pointer variables store the memory address of another variable. It doesn't store any value. Hence, there are only a few operations that are allowed to perform on Pointers in C language. The C pointer
10 min read
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 Content Incrementing and Decrementing Pointer in C++Addition of Constant to Poi
7 min read
C++ Multidimensional Array
A multidimensional array is an array with more than one dimension. It means that it can grow in different directions i.e. instead of changing the length only, it can also change in width, depth or more. It is the homogeneous collection of items where each element is accessed using multiple indices.
10 min read
4 Dimensional Array in C/C++
Prerequisite :Array in C/C++, More on array A four-dimensional (4D) array is an array of array of arrays of arrays or in other words 4D array is a array of 3D array. More dimensions in an array means more data be held, but also means greater difficulty in managing and understanding arrays. Declarati
3 min read
Initialization of Multidimensional Arrays in C++
In C++, multidimensional arrays are the type of arrays that have multiple dimensions, i.e., they can expand in multiple directions. In this article, we will discuss how to initialize the multidimensional arrays in C++. Methods to Initialize Multidimensional Array in C++We can initialize multidimensi
3 min read
Arithmetic Operators in C
Arithmetic operators are the type of operators used to perform basic math operations like addition, subtraction, and multiplication. Let's take a look at an example: [GFGTABS] C #include <stdio.h> int main() { // Calculate the area of the triangle int sum = 10 + 20; printf("%d", sum)
5 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
Multidimensional Arrays in C - 2D and 3D Arrays
Prerequisite: Arrays in C A multi-dimensional array can be defined as an array that has more than one dimension. Having more than one dimension means that it can grow in multiple directions. Some popular multidimensional arrays are 2D arrays and 3D arrays. In this article, we will learn about multid
10 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