Hi there,
I am having a problem of passing arrays to functions, here is my code:

void zeroMatrix(float **arr);
void main()
{

    float e[2][2];

    zeroMatrix(e);


}
void zeroMatrix(float** arr)
{ 
    int i,j;
    for (i=0;i<2;i++)
    {
        for(j=0;j<2;j++)
        {
            *( *(arr +i)+j) =0;
        }
    }
}

The problem is I want to allocate dynamically an array and to pass to a function.
Cheers guys

Edited 2 Years Ago by cppgangster: misspell

David W, thanks for posting the link. I have found about dynamically allocated arrays, howevr, it solves just half the problem

Edited 2 Years Ago by cppgangster

Here is some of my really old code ... that I found, and quickly cleaned up a little, so it would compile ok ... under C90 ... It that may give some more ideas?

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

typedef struct twoDArray
{
    int **p;
    int rows;
    int cols;
} matrix ;

typedef matrix* pMatrix;

/*
   'createMatrix' returns a pointer to a C struct ...
   The 'struct' holds a 'pointer to a pointer to int' for the 2-D array 
   AND ALSO holds the number of rows and cols for the 2-D array.
*/
pMatrix createMatrix( int, int );
void showMatrix( pMatrix );
void freeMatrix( pMatrix );



int main()
{
    int i, j;
    int r = 2, c = 4;
    int count = r*c*2;
    pMatrix pM1, pM2, pM3;

    pM1 = createMatrix( r, c );
    /* fill in some values ... */
    /* int i, j; */
    for( i = 0; i < pM1->rows; ++i )
        for( j  =0; j < pM1->cols; ++j )
            pM1->p[ i ][ j ] = count--;

    pM2 = createMatrix(r, c);
    /* fill in some values ... */
    for( i = 0; i < pM2->rows; ++ i )
        for( j = 0; j < pM2->cols; ++j )
            pM2->p[ i ][ j ] = count--;

    pM3 = createMatrix(r, c);
    /* sum first two matrices ...*/
    for( i = 0; i < pM2->rows; ++i )
        for( j = 0; j < pM2->cols; ++j )
            pM3->p[ i ][ j ] = pM2->p[ i ][ j ] + pM1->p[ i ][ j ];

    puts("");

    /* showTwoDArray( pAry ); */
    puts( "  Matrix1" );
    showMatrix(pM1 );
    puts( "\n+ Matrix2" );
    showMatrix(pM2 );
    puts( "\n= Matrix3" );
    showMatrix(pM3 );
    puts( "\nfree3" );
    freeMatrix(pM3 );
    puts( "\nfree2" );
    freeMatrix(pM2 );
    puts( "\nfree1" );
    freeMatrix(pM1 );
    puts( "" );
    system( "pause" );
    return 0;
}


pMatrix createMatrix( int numrows, int numcols )
{
    int i;
     pMatrix pM = (pMatrix)malloc( sizeof(matrix) );
     assert( pM );

     pM->p = (int**)malloc( sizeof(int*)*numrows );
     assert(pM->p);
     /* int i; // j; */
     for(i=0; i<numrows; ++i)
     {
          pM->p[ i ] = (int*)malloc( sizeof(int)*numcols );
          assert( pM->p[ i ] );
          /* //for( j = 0; j < numcols; ++j )
              //pM->p[i][j] = 0; // initialize all to zero  */
     }
     pM->rows = numrows;
     pM->cols = numcols;
     return pM;
}

void showMatrix( pMatrix pM )
{
    int i, j;
    for( i = 0; i < pM->rows; ++i )
    {
         for( j = 0; j < pM->cols; ++j )
              printf( "%4d ", pM->p[ i ][ j ] );
         puts( "" );
    }       
}

void freeMatrix( pMatrix pM )
{
     int i;
     for( i = pM->rows-1; i >= 0 ; --i )
     {
          printf( "pM->p[%d] = %d . ", i, (int)pM->p[ i ] );
          free( pM->p[ i ] ); 
     }    

     printf( "pM->p = %d . ", (int)pM->p );              
     free( pM->p );

     printf( "pM = %d",  (int)pM );
     free( pM );   
}

Edited 2 Years Ago by David W: added text

Or just do it like this:

void zeroMatrix(float arr[][2]);
void main()
{

    float e[2][2];

    zeroMatrix(e);


}
void zeroMatrix(float arr[][2])
{
    int i, j;
    for (i = 0; i<2; i++)
    {
        for (j = 0; j<2; j++)
        {
            *(*(arr + i) + j) = 0;
        }
    }
}
This article has been dead for over six months. Start a new discussion instead.