#include <stdio.h>

const int Nrows = 3;
const int Ncols = 3;

struct array
{
   int mat[Nrows][Ncols];
};

int testfun( array * );


int main()
{
   int mat[Nrows][Ncols] = {1,2,3,4,5,6,7,8,9};
   int i,j;
   int error = 0;
//
// Print it out:   
   for(i=0; i<Nrows; i++)
   {
      printf("Mat[%d][:] = %d %d %d\n",i, mat[i][0], mat[i][1], mat[i][2]);
   }
//
// Try indexing by rows:
   int *row1, *row2, *row3;
   row1 = &mat[0][0];
   row2 = &mat[1][0];
   row3 = &mat[2][0];

   printf("Row 1 = [ %d %d %d] \n", row1[0], row1[1], row1[2]);

   //
   // Write out a csv:
   const char *fileName = "testmat.csv";
   FILE *fidOut = fopen(fileName,"w");

   for(i=0; i<Nrows; i++)
   {
      fprintf(fidOut,"%d, %d, %d,\n", mat[i][0],mat[i][1],mat[i][2] );
   }
   fclose(fidOut);
//
// Now test passing a doubly subscripted array as a pointer to a struct:
   array a;
   array *pa = &a;

   for( i=0; i<Nrows; i++ )
      for( j=0; j<Ncols; j++ )
         pa->mat[i][j] = mat[i][j];

   error = testfun( pa );

   
   return error;
}

int testfun( struct array *pa )
{
   int error = 0;
   int i,j;
   if( NULL == pa )
   {
      printf("testfun: Error.  Null pointer.\n");
      return -1;
   }

      for(i=0; i<Nrows; i++)
   {
      printf("testfun: mat[%d,:] = [%d, %d, %d]\n", i, pa->mat[i][0],pa->mat[i][1],pa->mat[i][2] );
   }
   return error;
      
}
