2D arrays and pointers in ANSI C (summary)

lim at ecs.umass.edu lim at ecs.umass.edu
Tue Dec 4 09:07:53 AEST 1990


Thanks to all who answered my question about 2D arrays and pointers in ANSI C.
Here's a summary of what I'm using:

int main(void)
{
 double **matrix    /* matrix is a 1D array of pointers; each pointer repre- */
 int m = ?, n = ?;  /* sents the address of a 1D array of doubles.           */
 int i;
 

 /* For a m x n matrix */
 matrix = (double **) malloc(m * sizeof(double *));
 
 /* I use calloc to initialize the elements to zero */
 for ( i = 0; i < m; i++ )
   matrix[i] = (double *) calloc(n,sizeof(double));

 test_matrix(matrix,m,n);
}

void test_matrix(double **matrix, int m, int n)
{
 int i, j;

 for ( i = 0; i < m; i++ )
   { 
    for ( j = 0; j < n; j++ )
      matrix[i][j] = 4.0;
   }

 .
 .
 .
}


Jonathan Lim



More information about the Comp.lang.c mailing list