From: kagel AT quasar DOT bloomberg DOT com Date: Mon, 3 Feb 1997 12:15:02 -0500 Message-Id: <9702031715.AA21964@quasar.bloomberg.com > To: an118 AT chebucto DOT ns DOT ca Cc: djgpp AT delorie DOT com In-Reply-To: <5d2sdh$jhj@News.Dal.Ca> (an118@chebucto.ns.ca) Subject: Re: NEED HELP passing multi-dim arrays to functions Reply-To: kagel AT dg1 DOT bloomberg DOT com Errors-To: postmaster AT ns1 From: an118 AT chebucto DOT ns DOT ca (Graham Howard Wile) Newsgroups: comp.os.msdos.djgpp Date: 2 Feb 1997 20:11:29 GMT Organization: Chebucto Community Net Lines: 62 Nntp-Posting-Host: chebucto.ns.ca X-Newsreader: TIN [version 1.2 PL2] Dj-Gateway: from newsgroup comp.os.msdos.djgpp Content-Type: text Content-Length: 1322 I have a problem with passing multi-dimensional arrays to my function. Can anyone show me how to pass multi-dimensional arrays to functions? The following code always works fine for a single dimensional array, but not multi-dim's like the one in the example below (also, see my compiler output below that): #include void print_array(int *); void main() { int array1[3][3]; array1[0][0] = 1; array1[0][1] = 2; array1[0][2] = 3; array1[1][0] = 4; array1[1][1] = 5; array1[1][2] = 6; print_array(array1); } void print_array(int * par_array1) If the second subscript is fixed, ie always 3, you can use: void print_array( int *par_array1[3] ) and you should pass the magnitude of the first dimension unless it is fixed also. Otherwise, if both dimensions are variable, the function to handle this would be: void print_array(int * par_array1, int rows, int cols) ^^ # of elems in second dimension ^^ # of elems in first dimension { int d1, d2; for (d1=0; d1 < rows; d1++) for (d2=0; d2 < cols; d2++) cout << "array1[" << d1 <<"][" << d2 <<"]:\t" << par_array1[(d1*(cols-1)+d2] << endl; /* ^^^^^^^^^^^^^^^ - Here's the relevant calculation. */ } Here's what I get for my compiler errors: c:\c_progra.ms>gxx testing.cc -o testing.exe testing.cc: In function `int main(...)': testing.cc:19: passing `int (*)[3]' as argument 1 of `print array(int *)' testing.cc: In function `void print_array(int *)': This one it gives for lines 26-28, 30-32... testing.cc:26: invalid types `int[int]' for array subscript -- Art S. Kagel, kagel AT quasar DOT bloomberg DOT com A proverb is no proverb to you 'till life has illustrated it. -- John Keats