Path: utzoo!attcan!uunet!zaphod.mps.ohio-state.edu!julius.cs.uiuc.edu!apple!bionet!ames!ucsd!chem.ucsd.edu!tps From: tps@chem.ucsd.edu (Tom Stockfisch) Newsgroups: comp.lang.c Subject: Re: How to pass arbitrary 2-d array argument ? Message-ID: <964@chem.ucsd.EDU> Date: 8 Jan 91 05:35:54 GMT References: <1991Jan6.044056.23028@noose.ecn.purdue.edu> Reply-To: tps@chem.ucsd.edu (Tom Stockfisch) Organization: Chemistry Dept, UC San Diego Lines: 43 In article <1991Jan6.044056.23028@noose.ecn.purdue.edu> luj@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@chem.ucsd.edu