Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • C

Idiom #27 Create a 3-dimensional array

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

A 3D matrix with iteration variables i for rows, j for columns, k for depth
#include <stdlib.h>
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));
	}
}

Uses dynamic allocation.

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

double x[m][n][p];
X : array (1 .. M, 1 .. N, 1 .. P) of Float := (others => (others => (others => 1.0)));

New implementation...
< >
programming-idioms.org