Logo

Programming-Idioms

History of Idiom 26 > diff from v30 to v31

Edit summary for version 31 by programming-idioms.org:
[C] Less braces

Version 30

2016-09-20, 13:55:21

Version 31

2016-11-04, 11:53:50

Idiom #26 Create a 2-dimensional array

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

Illustration

Idiom #26 Create a 2-dimensional array

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

Illustration
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
This uses dynamic allocation.
Comments bubble
This uses dynamic allocation.