Tuesday 9 February 2010

2d dynamic array to be passed in a C function

To allocate it:

float **out=(float **)malloc(nRows * sizeof(float *));
for(unsigned int i = 0; i < nRows; i++)
out[i]=(float *)malloc(nCols * sizeof(float));

To free it:

for(unsigned int i = 0; i < nRows; i++)
free(out[i]);
free(out);

Monday 8 February 2010

From http://www.physicsforums.com/showpost.php?p=1948692&postcount=4

Re: Passing a 2d array to a function in C

#include
#include
#include
// Ref : http://www.eskimo.com/~scs/cclass/int/sx9b.html
void printArray(int **array, int m, int n)
{
for(int i=0;i}
int main()
{
int i,j,k=0, m=5, n=20;
int **a=(int **)malloc(m*sizeof(int *));
for(i=0;i for(i=0;i //for(i=0;i printArray(a,m,n);
system("PAUSE");
return 0;
}