Understanding Pointers with Two-Dimensional Arrays

 

In C programming, pointers play a crucial role in managing memory and accessing elements within arrays. A two-dimensional array, for instance, is a data structure that can be thought of as a matrix or a table with rows and columns. In this article, we will delve deep into how pointers interact with two-dimensional arrays, and how to effectively use them in C programming. We will explore the relationship between pointers and arrays and learn how to access elements, manipulate memory, and work with pointer arithmetic for two-dimensional arrays.

Table of Contents

1. Introduction to Pointers and Arrays
2. Declaring a Two-Dimensional Array
3. Creating Pointers for Two-Dimensional Arrays
4. Accessing Elements of a Two-Dimensional Array with Pointers
5. Address Mapping and Pointer Arithmetic
6. Understanding the Different Types of Pointers
7. Assigning Two-Dimensional Arrays to Pointer Variables
8. Using Pointers with Multi-Dimensional Arrays
9. Memory Allocation for Two-Dimensional Arrays
10. Conclusion

Introduction to Pointers and Arrays

In C programming, a pointer is a variable that holds the memory address of another variable. Pointers allow us to indirectly access and manipulate variables, allowing for more efficient and dynamic memory management. Arrays, on the other hand, are data structures that store multiple elements of the same data type. One-dimensional arrays store elements in a linear sequence, while two-dimensional arrays can be visualized as a matrix or a table with rows and columns.

The relationship between pointers and arrays is crucial in understanding how to work with two-dimensional arrays. In this article, we will explore how pointers can be used to access and manipulate elements within two-dimensional arrays.

Declaring a Two-Dimensional Array

A two-dimensional array can be declared in C using the following syntax:

data_type array_name[row_size][column_size];

For example, to declare a 3×4 integer array, we can use the following code:

int num[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} };

In this example, num is a two-dimensional integer array with 3 rows and 4 columns. The elements are stored in row-major order, which means that the first element of the second row is placed right after the last element of the first row.

Creating Pointers for Two-Dimensional Arrays

To create a pointer for a two-dimensional array, we first need to determine the type of the pointer. Since our example array num consists of integers, our pointer should also be of type int. We can assign the address of the first element of the array num to the pointer ptr using the address-of & operator:

int *ptr = &num[0][0];

The pointer ptr now holds the memory address of the first element of the two-dimensional array.

Accessing Elements of a Two-Dimensional Array with Pointers

A two-dimensional array is stored as a continuous block in memory. Therefore, if we increment the value of ptr by 1, we will move to the next block in the allocated memory.

The following code demonstrates how to print the contents of the num array using a for loop and by incrementing the value of ptr:

#include <stdio.h>
int main(void) 
{
   // 2d array
   int num[3][4] = { {1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12} };
   // pointer ptr pointing at array num
   int *ptr = &num[0][0];
   // other variables
   int ROWS = 3, COLS = 4, TOTAL_CELLS = ROWS * COLS, i;
   // print the elements of the array num via pointer ptr
   for (i = 0; i < TOTAL_CELLS; i++) 
   {
      printf("%d ", *(ptr + i));
   }
   return 0;
}

This code will output the following:

1 2 3 4 5 6 7 8 9 10 11 12

 

GrowthLadder Article Pointer with two dimensional array

 

Address Mapping and Pointer Arithmetic

We can compute the memory address of an element in the array by using the rows and columns of the array. The formula for calculating the address of a specific element in a two-dimensional array is:

arr[i][j] = base-Address + [(i * no_of_cols + j) * size_of_data_type]

In this formula, arr is a two-dimensional array, and i and j denote the ith row and jth column of the array, respectively. base-Address is the memory address of the first element of the array, and no_of_cols is the total number of columns in a row. The size_of_data_type is the size of the type of the pointer (e.g., 2 bytes for int and 1 byte for char).

For example, in the case of the num array, the baseAddress = 1000, no_of_cols = 4, and size_of_data_type = 2. We can compute the memory address location of the element num[2][3] as follows:

// address of element at cell 2,3
num[2][3] = base-Address + [(i * no_of_cols + j) * size_of_data_type] = 1000 + [(2 * 4 + 3) * 2] = 1000 + [(8 + 3) * 2] = 1000 + 22 = 1022


Understanding the Different Types of Pointers 

There are several types of pointers that we can work with when dealing with two-dimensional arrays:
1. Whole 2D array pointer: &arr is a pointer to the entire 2D array. Its base type is (int*)[row][col]. When incremented by 1, it points to the next 2D block in memory.
2. 1D array pointer: arr is a pointer to the first 1D array within the 2D array. Its base type is (int*)[row]. When incremented by 1, it points to the next 1D array in the 2D array.
3. Single element pointer: *arr is a pointer to the first element of the 2D array. Its base type is (int*). When incremented by 1, it points to the next element in the array.
4. Value of the first element: **arr is the value of the first element. When incremented by 1, it adds 1 to the value of the first element.


Assigning Two-Dimensional Arrays to Pointer Variables

We can assign the name of the array to a pointer variable, but unlike 1-D arrays, we need a pointer to an array instead of a pointer to int. For example, if we have a 2-D array with 3 rows and 4 columns like int arr[3][4], then we need a pointer to an array of 4 integers:

 

int (*p)[4];

Here, p is a pointer to an array of 4 integers. According to pointer arithmetic, p+i points to the ith 1-D array. On dereferencing p+i, we get the base address of the ith 1-D array, but now the base type of *(p + i) is a pointer to int or (int*). To access the address of the jth element in the ith 1-D array, we just have to add j to *(p + i). So *(p + i) + j points to the address of the jth element of the ith 1-D array. Therefore, the expression *(*(p + i) + j) gives the value of the jth element of the ith 1-D array.

 


Using Pointers with Multi-Dimensional Arrays

The concepts we’ve discussed so far for two-dimensional arrays can also be applied to multi-dimensional arrays. In general, a multi-dimensional array is an array where each element is an array itself.

When working with multi-dimensional arrays and pointers, it is essential to understand the base type of the pointer and how it relates to the dimensions of the array. This knowledge will allow you to access and manipulate elements within the array effectively.

Memory Allocation for Two-Dimensional Arrays

Memory allocation for two-dimensional arrays is performed in row-major order. This means that the elements of the first row are stored in consecutive memory blocks, followed by the elements of the second row, and so on.

Understanding how memory is allocated for two-dimensional arrays is crucial when working with pointers, as it allows you to perform pointer arithmetic and access elements within the array correctly.

Conclusion

In this article, we have explored the relationship between pointers and two-dimensional arrays in C programming. We have learned how to declare and create pointers for two-dimensional arrays, access elements within the array using pointers, and perform pointer arithmetic to manipulate memory. We have also discussed the different types of pointers and how they relate to multi-dimensional arrays. By understanding these concepts, you can effectively work with two-dimensional arrays and pointers in C programming.

 

**This is the overall concept of 2D Array with Pointers, we will discuss the string with pointers in a separate article in detail.**

You May Also Like…

Single Linked List in C

Single Linked List in C

  Single Linked List is a fundamental data structure in the C programming language. It allows us to store and...

0 Comments