home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / c / 16714 < prev    next >
Encoding:
Text File  |  1992-11-17  |  1.7 KB  |  53 lines

  1. Newsgroups: comp.lang.c
  2. Path: sparky!uunet!utcsri!newsflash.concordia.ca!IRO.UMontreal.CA!CC.UMontreal.CA!sifon!thunder.mcrcim.mcgill.edu!mouse
  3. From: mouse@thunder.mcrcim.mcgill.edu (der Mouse)
  4. Subject: Re: 2-D Arrays, Revisited.
  5. Message-ID: <1992Nov18.102604.8039@thunder.mcrcim.mcgill.edu>
  6. Organization: McGill Research Centre for Intelligent Machines
  7. References: <17489@pitt.UUCP>
  8. Date: Wed, 18 Nov 92 10:26:04 GMT
  9. Lines: 43
  10.  
  11. In article <17489@pitt.UUCP>, fahad@cs.pitt.edu (Fahad A Hoymany) writes:
  12.  
  13. > I am writing a function that should accept a 2-D array (hopefully of
  14. > any size), [...the argument array must be a 2-D array, not done with
  15. > an array of pointers...].
  16.  
  17. > What should the formal parameters line look like?
  18.  
  19. This cannot be done without knowing the size of all but the first
  20. dimension at compile time.  To use your "int a[2][5]" example, you have
  21. to know the 5 at compile time.  The formal parameter line can be
  22. deduced easily enough:
  23.  
  24. - argument is of type int [2][5].
  25. - argument is an array in an rvalue context, so it decays to a pointer
  26.    to its first element; new type is thus int (*)[5].
  27. - formal parameter type must match (modulo a couple of quibbles that
  28.    I'd rather not go into here).
  29.  
  30. Thus, the formal parameter must have type int (*)[5]:
  31.  
  32.     sometype dosomething(int (*array)[5])
  33.     {
  34.      ...
  35.     }
  36.  
  37. or, if you want to be portable to pre-prototype compilers,
  38.  
  39.     sometype dosomething(array)
  40.     int (*array)[5];
  41.     {
  42.      ...
  43.     }
  44.  
  45. Notice the 5 must be specified.  If you try to write "int (*array)[]"
  46. instead, the resulting pointer cannot really be used; the only value
  47. for the first subscript that makes any sense is 0, since the size of
  48. the pointed-to thing is not known.
  49.  
  50.                     der Mouse
  51.  
  52.                 mouse@larry.mcrcim.mcgill.edu
  53.