How to pass arbitrary 2-d array argument ?
Tom Stockfisch
tps at chem.ucsd.edu
Tue Jan 8 16:35:54 AEST 1991
In article <1991Jan6.044056.23028 at noose.ecn.purdue.edu> luj at delta.ecn.purdue.edu (Jun Lu) writes:
>It would be nice if I can have a "subroutine" which takes arbitrary matrices as
>arguments and performs some operations on them. The dimensions of the 2-d
>array are not known a priori.
I'm so proud of my clever programming constructs for this I just have to
post every time this topic comes up (-:
Using your code with my method for handling 2-d arrays in C,
main()
{
double m1[4][2], m2[4][2], m[4][2];
/* init m1, m2 ... */
madd(m1[0], m2[0], m[0], 4, 2);
/* ... */
}
madd( a, b, c, m, n )
double a[/* m*n */], b[/* m*n */], c[/* m*n */];
# define A(i,j) ( (i) *n+ (j) )
# define B(i,j) ( (i) *n+ (j) )
{
int i, j;
for (i = 0; i < m; i++) /* fucntionality of madd() */
for (j = 0; j < n; j++)
C(i,j) = A(i,j) + B(i,j);
}
Note the missing declarations for m,n. This is because they appear in
the comments inside the array dimensions.
Note the macro definitions are analogues of the Fortran "dimension" statement.
>No calling Fortran from C please.
Certainly not for this.
--
|| Tom Stockfisch, UCSD Chemistry tps at chem.ucsd.edu
More information about the Comp.lang.c
mailing list