C / C++ FAQs & Programming Resources - ProkutFAQ : MultiDimArray

HomePage Recent Changes Recently Commented Login/Register

Revision [910]

Most recent edit made on 2009-11-18 15:54:05 by SharathAV

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:




Revision [909]

Edited on 2009-11-16 09:54:14 by LeiMason (unregistered user)

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.




Revision [908]

Edited on 2009-11-16 09:53:29 by LeiMason (unregistered user)

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:




Revision [907]

Edited on 2009-11-16 09:52:41 by LeiMason (unregistered user)

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:




Revision [887]

Edited on 2009-10-30 19:39:41 by SharathAV

Additions:

Related Links

comp.lang.c FAQ section on allocating multidimensional array.




Revision [843]

Edited on 2009-07-27 22:41:19 by SharathAV

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.




Revision [842]

Edited on 2009-07-27 10:17:38 by SharathAV

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.




Revision [841]

Edited on 2009-07-25 02:47:33 by SharathAV

Additions:

Click on the image for an enlarged view.


Deletions:
<br/>

<br/>




Revision [840]

Edited on 2009-07-25 02:40:29 by SharathAV

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.




Revision [839]

Edited on 2009-07-25 02:27:08 by SharathAV

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;
}




Revision [838]

Edited on 2009-07-24 23:35:43 by SharathAV

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.




Revision [788]

Edited on 2009-02-22 02:01:49 by SharathAV

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;




Revision [787]

Edited on 2009-02-22 01:41:23 by SharathAV

Additions:
CategoryPointers




Revision [783]

Edited on 2009-02-21 14:31:58 by SharathAV

Additions:

CategoryArrays




Revision [759]

Edited on 2008-11-27 10:47:02 by SharathAV

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.*/




Revision [755]

Edited on 2008-10-29 07:14:38 by SharathAV

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




Revision [754]

Edited on 2008-10-29 03:38:30 by SharathAV [Made the code sample C90 Compatible]

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:

intarray2d; 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
}




Revision [720]

Edited on 2008-09-19 09:53:17 by SharathAV

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
}




Revision [701]

Edited on 2008-07-24 01:34:44 by SharathAV

Additions:

How to allocate a multi-dimensional (2D/3D) array dynamically using malloc()?



Deletions:

How to allocate a multidimensional array using malloc?





Revision [700]

The oldest known version of this page was edited on 2008-07-16 04:50:30 by SharathAV

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;
}

Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by WikkaWiki
Page was generated in -0.8724 seconds