Retraction (matrix routine)

Rick Busdiecker rfb at h.cs.cmu.edu
Tue Nov 19 00:39:14 AEST 1985


In a previous post I said that the declarations:

	double matrix [m][n]

and

	double (matrix [m])[n]

were not equivalent.  Duane Williams pointed out and cc convinced me that I
was wrong.  In order to treat a two dimensional array as **matrix you need
to set the pointers up yourself.  The routine make_mat() in the following
program works (I tested it this time!).  This type of matrix is slightly
more versatile than the one you get with a standard declaration because it
contains the standard representation which you can refer to as (test+m) when
passing it to a function which takes standard matrices.



#define HEIGHT	2
#define WIDTH	3

char *valloc ();

double **make_mat (m, n)
{
    double	**result;
    int		i;

    result = (double **)valloc (m * (n + 1));
    for (i = 0; i < m; i++)
        *(result + i) = (double *)result + m + i * n;
    return (result);
}


main ()
{
    double	**test;
    int		i, j;

    test = make_mat (HEIGHT, WIDTH);
    for (i = 0; i < HEIGHT; i++)
	for (j = 0; j < WIDTH; j++)
	{
	    test [i][j] = i * WIDTH + j;
	    printf ("test [%d][%d] = %g\t%d\n", i, j, test [i][j],
		    (&(test [i][j]) - &(test [0][0])));
	}
    putchar ('\n');
    for (i = 0; i < HEIGHT; i++)
	for (j = 0; j < WIDTH; j++)
    	    printf ("test [%d][%d] = %g\n", i, j, test [i][j]);
}



Sorry for any confusion I caused people.

---------------------------------------------------------------------------
 Rick Busdiecker                       ARPA:    rfb at h.cs.cmu.edu           
 Carnegie-Mellon University            UUCP:    ...!seismo!h.cs.cmu.edu!rfb 
 Mathematics Department                AT&T:    (412) 521-1459             
                                       USPS:    4145 Murray Ave. 15217     
---------------------------------------------------------------------------



More information about the Comp.sources.unix mailing list