How to declare a Two Dimensional Array of pointers in C?
Last Updated :
29 Jun, 2022
A Two Dimensional array of pointers is an array that has variables of pointer type. This means that the variables stored in the 2D array are such that each variable points to a particular address of some other element.
How to create a 2D array of pointers:
A 2D array of pointers can be created following the way shown below.
int *arr[5][5]; //creating a 2D integer pointer array of 5 rows and 5 columns.
The element of the 2D array is been initialized by assigning the address of some other element.
In the example, we have assigned the address of integer variable 'n' in the index (0, 0) of the 2D array of pointers.
int n; //declared a variable
arr[0][0] = &n; //assigned a variable at position (0, 0)
Below is the implementation of the 2D array of pointers.
C
#include <stdio.h>
// Drivers code
int main()
{
int arr1[5][5] = { { 0, 1, 2, 3, 4 },
{ 2, 3, 4, 5, 6 },
{ 4, 5, 6, 7, 8 },
{ 5, 4, 3, 2, 6 },
{ 2, 5, 4, 3, 1 } };
int* arr2[5][5];
// Initialising each element of the
// pointer array with the address of
// element present in the other array
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
arr2[i][j] = &arr1[i][j];
}
}
// Printing the array using
// the array of pointers
printf("The values are\n");
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
printf("%d ", *arr2[i][j]);
}
printf("\n");
}
return 0;
}
OutputThe values are
0 1 2 3 4
2 3 4 5 6
4 5 6 7 8
5 4 3 2 6
2 5 4 3 1
A two-dimensional array of pointers can also be created using Dynamic Memory Allocation.
We can use the malloc() function to dynamically allocate memory.
ptr = (cast-type*) malloc(byte-size)
Below is the implementation of a 2D array of pointers using Dynamic Memory Allocation.
C
#include <stdio.h>
#include <stdlib.h>
// Drivers code
int main()
{
int arr1[5][5] = { { 0, 1, 2, 3, 4 },
{ 2, 3, 4, 5, 6 },
{ 4, 5, 6, 7, 8 },
{ 5, 4, 3, 2, 6 },
{ 2, 5, 4, 3, 1 } };
// Creating 2D array of pointers using Dynamic Memory
// allocation through malloc() function
int*** arr2 = malloc(5 * sizeof(int**));
for (int i = 0; i < 5; i++)
arr2[i] = malloc(5 * sizeof(int*));
// Initialising each element of the
// pointer array with the address of
// element present in the other array
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
arr2[i][j] = &arr1[i][j];
}
}
// Printing the array using
// the array of pointers
printf("The values are\n");
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
printf("%d ", *arr2[i][j]);
}
printf("\n");
}
return 0;
}
Output:
The values are
0 1 2 3 4
2 3 4 5 6
4 5 6 7 8
5 4 3 2 6
2 5 4 3 1
Similar Reads
How to Declare 3-Dimensional Arrays in Wiring in C? Prerequisite: Array in C Arrays are used the make a list of the same data type elements and store it in contiguous memory locations. In simple words, arrays are the organization of elements of the same data types. There are many types for declaring the array i.e, 1-Dimensional arrays, 2-Dimensional
2 min read
How to Declare a Pointer to a Function? A pointer to a function is similar to a pointer to a variable. However, instead of pointing to a variable, it points to the address of a function. This allows the function to be called indirectly, which is useful in situations like callback functions or event-driven programming.In this article, we w
2 min read
How to Declare and Initialize an Array of Pointers to a Structure in C? Prerequisite: Structure in CArray in C In C language, arrays are made to store similar types of data in contiguous memory locations. We can make arrays of either primitive data types, like int, char, or float, or user-defined data types like Structures and Unions. We can also make arrays of pointers
8 min read
Difference Between one-dimensional and two-dimensional array Array is a data structure that is used to store variables that are of similar data types at contiguous locations. The main advantage of the array is random access and cache friendliness. There are mainly three types of the array: One Dimensional (1D) ArrayTwo Dimension (2D) ArrayMultidimensional Arr
3 min read
One Dimensional Arrays in C In C, an array is a collection of elements of the same type stored in contiguous memory locations. This organization allows efficient access to elements using their index. Arrays can also be of different types depending upon the direction/dimension they can store the elements. It can be 1D, 2D, 3D,
5 min read
One Dimensional Arrays in C++ One-dimensional arrays are like a row of boxes where you can store things where each box can hold one item, such as a number or a word. For example, in an array of numbers, the first box might hold 5, the second 10, and so on. You can easily find or change what's in each box by referring to its posi
6 min read