Logo

Programming-Idioms

History of Idiom 27 > diff from v5 to v6

Edit summary for version 6 by :

Version 5

2015-08-20, 10:25:33

Version 6

2015-08-20, 10:25:50

Idiom #27 Create a 3-dimensional array

Declare and initialize a 3D array x, having dimensions boundaries m, n, p, and containing real numbers.

Idiom #27 Create a 3-dimensional array

Declare and initialize a 3D array x, having dimensions boundaries m, n, p, and containing real numbers.

Imports
#include <stdlib.h>
Imports
#include <stdlib.h>
Code
double ***x=malloc(m*sizeof(double *));
int i,j;
for(i=0;i<m;i++)
{
	x[i]=malloc(n*sizeof(double));
	for(j=0;j<n;j++)
	{
		x[i][j]=malloc(p*sizeof(double));
	}
}
Code
double ***x=malloc(m*sizeof(double *));
int i,j;
for(i=0;i<m;i++)
{
	x[i]=malloc(n*sizeof(double));
	for(j=0;j<n;j++)
	{
		x[i][j]=malloc(p*sizeof(double));
	}
}
Comments bubble
Uses dynamic allocation.

If the values of m and n are known at compile time you can also use:

double x[m][n][p];