Additions:
Allocating multidimensional arrays using malloc has intimidated many newbies. This page hopes to clear the most basic doubts.
If you understand the above program to allocate 2D array, you can allocate arrays of any dimension. However, here's another example showing how to allocate a 3D array(you would almost never need bigger dimension than 3D). You will have to make use of pointer-to-pointer-to-pointer, instead of just pointer-to-pointer. Suppose we have to create a 3D array of order m*n*o, then here is how we can do:
Deletions:
Allocating multidimensional arrays using malloc has intimidated many newbies. This page hopes to clear the most basic doubts about its usage.
If you understand the above program to allocate 2D array, you can allocate arrays of any dimension according to the
custom essay∞. However, here's another example showing how to allocate a 3D array(you would almost never need bigger dimension than 3D). You will have to make use of pointer-to-pointer-to-pointer, instead of just pointer-to-pointer. Suppose we have to create a 3D array of order m*n*o, then here is how we can do:
Additions:
Allocating multidimensional arrays using malloc has intimidated many newbies. This page hopes to clear the most basic doubts about its usage.
Deletions:
Allocating multidimensional arrays using malloc has intimidated many newbies. This page hopes to clear the most basic doubts.
Additions:
If you understand the above program to allocate 2D array, you can allocate arrays of any dimension according to the
custom essay∞. However, here's another example showing how to allocate a 3D array(you would almost never need bigger dimension than 3D). You will have to make use of pointer-to-pointer-to-pointer, instead of just pointer-to-pointer. Suppose we have to create a 3D array of order m*n*o, then here is how we can do:
Deletions:
If you understand the above program to allocate 2D array, you can allocate arrays of any dimension according to the
custom essay∞. However, here's another example showing how to allocate a 3D array(you would almost never need bigger dimension than 3D). You will have to make use of pointer-to-pointer-to-pointer, instead of just pointer-to-pointer. Suppose we have to create a 3D array of order m*n*o, then here is how we can do:
Additions:
If you understand the above program to allocate 2D array, you can allocate arrays of any dimension according to the
custom essay∞. However, here's another example showing how to allocate a 3D array(you would almost never need bigger dimension than 3D). You will have to make use of pointer-to-pointer-to-pointer, instead of just pointer-to-pointer. Suppose we have to create a 3D array of order m*n*o, then here is how we can do:
Deletions:
If you understand the above program to allocate 2D array, you can allocate arrays of any dimension. However, here's another example showing how to allocate a 3D array(you would almost never need bigger dimension than 3D). You will have to make use of pointer-to-pointer-to-pointer, instead of just pointer-to-pointer. Suppose we have to create a 3D array of order m*n*o, then here is how we can do:
Additions:
Additions:
You can see the pointer expression to access each of the location allocated for the 2d array in the above image. The elements of the 2-dimensional array are contiguous memory locations and we can access this memory using a single subscript notation or in-fact, even without the subscript notation.
Deletions:
You can see find pointer expression to access each of the location allocated for the 2d array in the above example. The elements of the 2-dimensional array are contiguous memory locations and we can access this memory using a single subscript notation or in-fact, even without the subscript notation.
Additions:
The elements of the dynamic array can be accessed with array subscripts like p[i][j], under the hood p[i][j] is translated to *(*(p+i)+j)).
Below is a graphical view of the memory allocation for the 2d array given in the above example code for 3*3 matrix ( 3 rows and 3 columns). Click on the image for an enlarged view.
You can see find pointer expression to access each of the location allocated for the 2d array in the above example. The elements of the 2-dimensional array are contiguous memory locations and we can access this memory using a single subscript notation or in-fact, even without the subscript notation.
Deletions:
The elements of the dynamic array can be accessed with array subscripts like p[i][j], under the hood p[i][j] is translated to *(*(p+i)+j)).
Below is a graphical view of the memory allocation for the 2d array given in the above example code.
Click on the image for an enlarged view.
The elements of the 2-dimensional array are contiguous memory locations and we can access this memory using a single subscript notation or in-fact, even without the subscript notation.
Additions:
Click on the image for an enlarged view.
Deletions:
<br/>

<br/>
Additions:
int
p; /*declaration of p as: pointer-to-pointer of int */
p = malloc( row * sizeof(*p) ); /*Allocate integer pointers for the rows */
if(p != NULL)
/* Set p[i] pointer to a block of memory for 'column' number of integers */
p[i] = malloc(column * sizeof p); /*Here, sizeof(p) is same as sizeof(int) */
if(p[i] NULL)
p[i][j]=i*column+j+1; /* normal-looking array subscripts */
printf("%d", p[i][j]);
The elements of the dynamic array can be accessed with array subscripts like p[i][j], under the hood p[i][j] is translated to *(*(p+i)+j)).
Below is a graphical view of the memory allocation for the 2d array given in the above example code.
<br/>

<br/>
The elements of the 2-dimensional array are contiguous memory locations and we can access this memory using a single subscript notation or in-fact, even without the subscript notation.
Deletions:
int
array2d; /*declaration of array2d as: pointer-to-pointer of int */
array2d = malloc( row * sizeof(*array2d) ); /*Allocate integer pointers for the rows */
if(array2d != NULL)
/* Set array2d[i] pointer to a block of memory for 'column' number of integers */
array2d[i] = malloc(column * sizeof array2d); /*Here, sizeof(array2d) is same as sizeof(int) */
if(array2d[i] NULL)
array2d[i][j]=i*column+j+1;/* normal-looking array subscripts */
printf("%d", array2d[i][j]);
The elements of the dynamic array can be accessed with array subscripts like array[i][j], under the hood array[i][j] is translated to *(*(array+i)+j)).
The elements of the 2-dimensional array are contiguous memory locations and we can access this memory using a single subscript notation or infact even without the subscript notation.
Additions:
#include<stdlib.h>
for(i = 0; i < row; i++) /* Loop through each row pointer to allocate memory for columns*/
/* Set array2d[i] pointer to a block of memory for 'column' number of integers */
array2d[i] = malloc(column * sizeof
array2d); /*Here, sizeof(array2d) is same as sizeof(int) */
if(array2d[i]
NULL)
printf("Memory allocation failed. Exiting....");
return 1;
else
printf("Memory allocation failed. Exiting....");
return 1;
/*You can access values in the dynamically allocated array in the same
way you do for other arrayssubscripting rows and columns as below */
for(i=0;i<row;i++)
for(j=0;j<column;j++)
{
array2d[i][j]=i*column+j+1;/* normal-looking array subscripts */
printf("%d", array2d[i][j]);
}
printf("\n");
return 0;
}
Deletions:
#include<stdio.h>
#include <stdlib.h>
int main()
for(i = 0; i < row; i++) /* Loop through each row pointer to allocate memory for columns
/* Set array2d[i] pointer to a block of memory for 'column' number of integers */
array2d[i] = malloc(column * sizeof
array2d); /*Here, sizeof(array2d) is same as sizeof(int) */
if(array2d[i]
NULL)
{
printf("Memory allocation failed. Exiting....");
return 1;
}
else
printf("Memory allocation failed. Exiting....");
return 1;
/*You can access values in the dynamically allocated array in the same
way you do for other arrayssubscripting rows and columns as below */
for(i=0;i<row;i++)
for(j=0;j<column;j++)
array2d[i][j]=i*column+j+1;/* normal-looking array subscripts */
return 0;
}
Additions:
To allocate a multidimensional array, we have to allocate an array of pointers following which we have to initialize each pointer to a dynamically-allocated row, using a pointer-to-pointer.
Deletions:
To allocate a multidimensional array we have to allocate an array of pointers following which we have to initialize each pointer to a dynamically-allocated row, using a pointer-to-pointer.
Additions:
int main()
for(i = 0; i < row; i++) /* Loop through each row pointer to allocate memory for columns
{
/* Set array2d[i] pointer to a block of memory for 'column' number of integers */
array2d[i] = malloc(column * sizeof
array2d); /*Here, sizeof(array2d) is same as sizeof(int) */
if(array2d[i]
NULL)
{
}
else
{
/*You can access values in the dynamically allocated array in the same
way you do for other arrayssubscripting rows and columns as below */
for(i=0;i<row;i++)
{
for(j=0;j<column;j++)
{
return 0;
Deletions:
int main(){
for(i = 0; i < row; i++){
array2d[i] = malloc(column * sizeof
array2d);/* array2d[i] is a pointer to a block of memory large enough to hold column' integers */
if(array2d[i]
NULL){
else{
/*Accessing this 2 dimensional array*/
for(i=0;i<row;i++){
for(j=0;j<column;j++){
return 0;
Additions:
Additions:
Additions:
The elements of the dynamic array can be accessed with array subscripts like array[i][j], under the hood array[i][j] is translated to *(*(array+i)+j)).
int x, y, z;
printf("Enter the order of the 3d matrix(x, y, z):\n");
scanf("%d%d%d",&x,&y,&z);
/* Validate input values */
arr3d=malloc(x*sizeof *arr3d );/* Allocate 'x' number of pointer-to-pointers to int */
arr3d[ i ] = malloc(y* sizeof
arr3d);/* Allocate 'y' number of pointers to int */
/*Validate malloc's success/failure using the return value*/
arr3d[ i ][ j ]= malloc(z* sizeof *arr3d);/* Allocate 'z' number of ints */
/*Validate malloc's success/failure using the return value*/
/*Note, the error checking of malloc return value is excluded, look above example to know how to do it.*/
Deletions:
The elements of the dynamic array can be accessed with array subscripts like array[i][j].
Under the hood a call like array[i][j] is translated to *(*(array+i)+j)).
int m, n, o;
printf("Enter the order of the 3d matrix:\n");
scanf("%d%d%d",&m,&n,&o);
arr3d=malloc(m*sizeof *arr3d );/* Allocate 'm' pointer-to-pointer of int */
arr3d[ i ] = malloc(n* sizeof
arr3d);/* Allocate 'n' pointers of int */
arr3d[ i ][ j ]= malloc(o* sizeof *arr3d);/* Allocate 'o' ints */
/*Note, the error checking of malloc return value has to be included here, look above example to know how to do it.*/
Additions:
int
*arr3d; /* Declaration of arr3d as: pointer-to-pointer-to-pointer of int */
int i, j;
arr3d=malloc(m*sizeof *arr3d );/* Allocate 'm' pointer-to-pointer of int */
for( i=0; i<m ; i++ )
arr3d[ i ] = malloc(n* sizeof arr3d);/* Allocate 'n' pointers of int */
for( j=0; j<n; j++)
arr3d[ i ][ j ]= malloc(o* sizeof *arr3d);/* Allocate 'o' ints */
Deletions:
int
*arr3d; Declaration of arr3d as: pointer-to-pointer-to-pointer of int
arr3d=malloc(m*sizeof *arr2d );Allocate 'm' pointer-to-pointer of int
for(int i=0;i<m;i++)
arr3d[ i ] = malloc(n* sizeof arr3d*));
Allocate 'n' pointers of int
for(int j=0;j<n;j++)
arr3d[ i ][ j ]= malloc(o* sizeof *arr3d);Allocate 'o' ints
Additions:
int
array2d; /*declaration of array2d as: pointer-to-pointer of int */
int i, j; /*Indexing variables*/
array2d = malloc( row * sizeof(*array2d) ); /*Allocate integer pointers for the rows */
if(array2d != NULL)
for(i = 0; i < row; i++){
array2d[i] = malloc(column * sizeof array2d);/* array2d[i] is a pointer to a block of memory large enough to hold column' integers */
printf("Memory allocation failed. Exiting....");
/*Accessing this 2 dimensional array*/
for(i=0;i<row;i++){
for(j=0;j<column;j++){
array2d[i][j]=i*column+j+1;/* normal-looking array subscripts */
}
Deletions:
int
array2d; declaration of array2d as: pointer-to-pointer of int
array2d = malloc(row * sizeof *array2d); Allocate integer pointers for the rows
if(array2d ! = NULL)
for(int i = 0; i < row; i++){
array2d[i] = malloc(column * sizeof array2d);array2d[i] is a pointer to a block of memory large enough to hold column' integers
printf("Memory allocation failed. Exiting....");
Accessing this 2 dimensional array
for(int i=0;i<row;i++){
for(int j=0;j<column;j++){
array2d[i][j]=i*column+j+1;normal-looking array subscripts
}
Additions:
How to allocate a multi-dimensional (2D/3D) array dynamically using malloc()?
array2d[i] = malloc(column * sizeof
array2d);array2d[i] is a pointer to a block of memory large enough to hold column' integers
if(array2d[i]
NULL){
printf("Memory allocation failed. Exiting....");
return 1;
printf("Memory allocation failed. Exiting....");
return 1;
array2d[i][j]=i*column+j+1;normal-looking array subscripts
}
arr3d[ i ] = malloc(n* sizeof arr3d*));
Allocate 'n' pointers of int
for(int j=0;j<n;j++)
{
arr3d[ i ][ j ]= malloc(o* sizeof *arr3d);Allocate 'o' ints
Deletions:
How to allocate a multi-dimensional (2D/3D) array dynamically using malloc()?
array2d[i] = malloc(column * sizeof array2d);array2d[i] is a pointer to a block of memory large enough to hold column' integers
if(array2d[i] NULL){
printf("Memory allocation failed. Exiting....");
return 1;
printf("Memory allocation failed. Exiting....");
return 1;
array2d[i][j]=i*column+j+1;normal-looking array subscripts
}
arr3d[ i ] = malloc(n* sizeof arr3d*));
Allocate 'n' pointers of int
for(int j=0;j<n;j++)
{
arr3d[ i ][ j ]= malloc(o* sizeof *arr3d);Allocate 'o' ints
}
Additions:
How to allocate a multi-dimensional (2D/3D) array dynamically using malloc()?
Deletions:
How to allocate a multidimensional array using malloc?
How to allocate a multidimensional array using malloc?
If you're not familiar with dynamic memory allocation, please read Dynamic Memory Allocation∞ page before you read this.
Allocating multidimensional arrays using malloc has intimidated many newbies. This page hopes to clear the most basic doubts.
To allocate a multidimensional array we have to allocate an array of pointers following which we have to initialize each pointer to a dynamically-allocated row, using a pointer-to-pointer.
Here is how you can allocate a two-dimensional array
#include<stdio.h>
#include <stdlib.h>
int main
(){
int row,column;
printf("Enter the number of rows and columns:\n");
scanf
("%d",&row
);
scanf
("%d",&column
);
int**array2d;
//declaration of array2d as: pointer-to-pointer of int
array2d = malloc
(row *
sizeof *array2d
);
//Allocate integer pointers for the rows
if(array2d ! =
NULL)
for(int i =
0; i < row; i++
){
array2d
[i
] = malloc
(column *
sizeof **array2d
);
//array2d[i] is a pointer to a block of memory large enough to hold column' integers
if(array2d
[i
] ==
NULL){
printf("Memory allocation failed. Exiting....");
return 1;
}
}
else{
printf("Memory allocation failed. Exiting....");
return 1;
}
//Accessing this 2 dimensional array
for(int i=
0;i<row;i++
){
for(int j=
0;j<column;j++
){
array2d
[i
][j
]=i*column+j
+1;
//normal-looking array subscripts
}
}
return 0;
}
The elements of the dynamic array can be accessed with array subscripts like array[i][j].
The elements of the 2-dimensional array are contiguous memory locations and we can access this memory using a single subscript notation or infact even without the subscript notation.
Under the hood a call like array[i][j] is translated to *(*(array+i)+j)).
Allocating 3D array
If you understand the above program to allocate 2D array, you can allocate arrays of any dimension. However, here's another example showing how to allocate a 3D array(you would almost never need bigger dimension than 3D). You will have to make use of pointer-to-pointer-to-pointer, instead of just pointer-to-pointer. Suppose we have to create a 3D array of order m*n*o, then here is how we can do:
#include<stdio.h>
#include <stdlib.h>
int main
()
{
int ***arr3d;
//Declaration of arr3d as: pointer-to-pointer-to-pointer of int
int m, n, o;
printf("Enter the order of the 3d matrix:\n");
scanf
("%d%d%d",&m,&n,&o
);
arr3d=malloc
(m*
sizeof *arr2d
);
//Allocate 'm' pointer-to-pointer of int
for(int i=
0;i<m;i++
)
{
arr3d
[ i
] = malloc
(n*
sizeof **arr3d*
));
//Allocate 'n' pointers of int
for(int j=
0;j<n;j++
)
{
arr3d
[ i
][ j
]= malloc
(o*
sizeof ***arr3d
);
//Allocate 'o' ints
}
}
/*Note, the error checking of malloc return value has to be included here, look above example to know how to do it.*/
/*For accessing the elements, its similar to any other 3D array... that is arr[i][j][k]; */
return 0;
}