prototyping (oh no! not again??)

Stan Brown browns at iccgcc.decnet.ab.com
Wed Nov 28 03:40:35 AEST 1990


In article <DAVIS.90Nov26144044 at pacific.mps.ohio-state.edu>, 
davis at pacific.mps.ohio-state.edu ("John E. Davis") writes:

>    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?
> 
The function was defined thusly:
> 
>    double trace(double **matrix, int dim)
>    {
	/* stuff deleted */
>    }

So you have a function that expects a pointer to a pointer to double.
Because of the PARTIAL equivalence of arrays and pointers in the
specific context of function definitions, you could also say that the
function expects an array of pointers to double.  Note that neither of
these is the same as an array of double--not a one-dimensional or a
two-dimensional array of double.

The function was called thusly:
> 
>    int main()
>    {
>      double m[10][10],t;
>      .
>      .
>      t = trace(m,10);
>      .
>      .
>    }
> 
> This dumps core because of segmentation fault.

m is a 2-D array of double.  This is not compatible with the definition
of the function above.

BTW, which compiler are you using?  It should have rejected the function
call if the prototype was in scope.  Catching stuff like this before it
core dumps is the reason we have prototypes.

The cure? Depends on what you're trying to accomplish.

With these hints, you might try reading the Frequently Asked Questions
again.  Pointers and arrays are not the same thing.  There are certain
cases where a first-level pointer and the name of a 1-D array are
treated as the same thing, but I can't think of any way that a
second-level pointer and a 2-D array could conform.

Please do not attribute these remarks to any other person or company.
                                   email: browns at iccgcc.decnet.ab.com
Stan Brown, Oak Road Systems, Cleveland, Ohio, USA    +1 216 371 0043



More information about the Comp.lang.c mailing list