prototyping (oh no! not again??)

John E. Davis davis at pacific.mps.ohio-state.edu
Tue Nov 27 06:40:44 AEST 1990


Hi,

   Sorry about this, I am sure many are sick of these type of questions.

   I have a function that takes a 2-d array of unknown dimension and does
   things with it.  How do I declare and call it?

   I tried this:

   double trace(double **matrix, int dim)
   {
      int i;
      double t;

      t = 0.0;
      i = 0;
      while(i++ < dim) t = t + matrix[i][i];
      return t;
   }
        

   int main()
   {
     double m[10][10],t;
     .
     .
     t = trace(m,10);
     .
     .
   }


This dumps core because of segmentation fault.  However, if I prototype trace
as:

    double trace(double matrix[10][10],int n)

then the program is fine. However, it looses its generality. What is wrong
with the first prototype? 

I am sure I am doing something wrong and there is a big lesson in store for
me.


         
--
John

  bitnet: davis at ohstpy
internet: davis at pacific.mps.ohio-state.edu



More information about the Comp.lang.c mailing list