home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!comp.vuw.ac.nz!actrix!David.Empson
- From: David.Empson@bbs.actrix.gen.nz
- Subject: Re: 2-D Arrays, Revisited.
- Organization: Actrix Information Exchange
- Date: Wed, 18 Nov 1992 12:23:42 GMT
- Message-ID: <1992Nov18.122342.17477@actrix.gen.nz>
- References: <17489@pitt.UUCP>
- Sender: David.Empson@actrix.gen.nz (David Empson)
- Lines: 43
-
- In article <17489@pitt.UUCP> fahad@cs.pitt.edu (Fahad A Hoymany) writes:
- > I am writing a function that should accept a 2-D array (hopefully of any
- > size), operate on that array, and returns 'anything' to the caller. Please
- > don't suggest I use double pointers because I do not want to restrict the
- > users of my function to use them when they are used to declaring arrays
- > as : int a[2][5].
- >
- > What should the formal parameters line look like?
-
- int my_array_function(int a[][5], int nrows) {
- /* ... */
- }
-
- You can refer to array elements within the function as normal, e.g.
- a[i][j] will refer to the ith row and jth column.
-
- The function should be called with the name of a compatible array.
-
-
- In C, only the leftmost dimension can be left unspecified. All other
- dimensions must be fixed. If you want a 2-D array that varies in both
- dimensions, you will have to do something rather more ugly, e.g.:
-
- int another_array_function(int a[], int nrows, int ncols) {
- /* To refer to element a[i][j], use: */
- temp = a[i * ncols + j];
- }
-
- and to call this function, given an array b[10][2], you would use:
-
- temp = another_array_function(b[0], 10, 2);
-
- or
-
- temp = another_array_function(&b[0][0], 10, 2);
-
-
- Hope this helps.
- --
- David Empson
-
- Internet: David.Empson@bbs.actrix.gen.nz EMPSON_D@kosmos.wcc.govt.nz
- Snail mail: P.O. Box 27-103, Wellington, New Zealand
-