home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: sparky!uunet!utcsri!newsflash.concordia.ca!IRO.UMontreal.CA!CC.UMontreal.CA!sifon!thunder.mcrcim.mcgill.edu!mouse
- From: mouse@thunder.mcrcim.mcgill.edu (der Mouse)
- Subject: Re: 2-D Arrays, Revisited.
- Message-ID: <1992Nov18.102604.8039@thunder.mcrcim.mcgill.edu>
- Organization: McGill Research Centre for Intelligent Machines
- References: <17489@pitt.UUCP>
- Date: Wed, 18 Nov 92 10:26:04 GMT
- 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), [...the argument array must be a 2-D array, not done with
- > an array of pointers...].
-
- > What should the formal parameters line look like?
-
- This cannot be done without knowing the size of all but the first
- dimension at compile time. To use your "int a[2][5]" example, you have
- to know the 5 at compile time. The formal parameter line can be
- deduced easily enough:
-
- - argument is of type int [2][5].
- - argument is an array in an rvalue context, so it decays to a pointer
- to its first element; new type is thus int (*)[5].
- - formal parameter type must match (modulo a couple of quibbles that
- I'd rather not go into here).
-
- Thus, the formal parameter must have type int (*)[5]:
-
- sometype dosomething(int (*array)[5])
- {
- ...
- }
-
- or, if you want to be portable to pre-prototype compilers,
-
- sometype dosomething(array)
- int (*array)[5];
- {
- ...
- }
-
- Notice the 5 must be specified. If you try to write "int (*array)[]"
- instead, the resulting pointer cannot really be used; the only value
- for the first subscript that makes any sense is 0, since the size of
- the pointed-to thing is not known.
-
- der Mouse
-
- mouse@larry.mcrcim.mcgill.edu
-