Logo

Programming-Idioms

History of Idiom 26 > diff from v18 to v19

Edit summary for version 19 by :
[C] Alternative code in comment -> new separate implementation

Version 18

2016-01-07, 14:11:43

Version 19

2016-01-07, 14:12:30

Idiom #26 Create a 2-dimensional array

Declare and initialize a matrix x having m rows and n columns, containing real numbers.

Idiom #26 Create a 2-dimensional array

Declare and initialize a matrix x having m rows and n columns, containing real numbers.

Imports
#include <stdlib.h>
Imports
#include <stdlib.h>
Code
double **x=malloc(m*sizeof(double *));
int i;
for(i=0;i<m;i++)
{
	x[i]=malloc(n*sizeof(double));
}
Code
double **x=malloc(m*sizeof(double *));
int i;
for(i=0;i<m;i++)
{
	x[i]=malloc(n*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];
Comments bubble
This uses dynamic allocation.