Newsgroups: comp.lang.c Path: utzoo!censor!geac!maccs!rory From: rory@maccs.dcss.mcmaster.ca (Rory Jacobs) Subject: Re: How to pass arbitrary 2-d array argument ? Message-ID: <27874031.11367@maccs.dcss.mcmaster.ca> Organization: McMaster University, Hamilton, Ontario, Canada References: <1991Jan6.044056.23028@noose.ecn.purdue.edu> Date: Sun, 6 Jan 91 15:20:17 GMT 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. In other words, how do you add two >matrices together portably in C ? ( see following scenario descriptions ) > >main() >{ > double m1[4][2], m2[4][2], m[4][2]; > double a[5][5], b[5][5], c[5][5]; > > /* init m1, m2 ... */ > > > madd(m1, m2, m, 4, 2); > madd(a, b, c, 5, 5); > > /* ... */ > >} > >madd(a, b, c, m, n) > /* args --- how ???(Note n is not known at compile time, but we assume that > the type of the 2-d array elements are known, i.e. double */ >{ > /* > * some work --- how ???? > */ > > for (i = 0; i < m; i++) /* fucntionality of madd() */ > for (j = 0; j < n; j++) > c[i][j] = a[i][j] + b[i][j]; > >} Use pointer arithemetic, as in the following way: /*-------------------*/ void madd(a,b,c,m,n) /*-------------------*/ double *a,*b,*c; int m,n; { for (i=0;i