home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #27 / NN_1992_27.iso / spool / comp / lang / c / 17086 < prev    next >
Encoding:
Internet Message Format  |  1992-11-24  |  2.4 KB

  1. Path: sparky!uunet!psinntp!dg-rtp!sheol!throopw
  2. From: throopw@sheol.UUCP (Wayne Throop)
  3. Newsgroups: comp.lang.c
  4. Subject: 2-D Arrays
  5. Message-ID: <722575658@sheol.UUCP>
  6. Date: 24 Nov 92 00:32:53 GMT
  7. References: <1992Nov23.010230.45040@kuhub.cc.ukans.edu>
  8. Lines: 53
  9.  
  10. : From: 2hujwant@kuhub.cc.ukans.edu
  11. : Message-ID: <1992Nov23.010230.45040@kuhub.cc.ukans.edu>
  12. :  our teacher told us that you define them [..2-d arrays..]
  13. : just like any other array, but with another index.  i.e.:
  14. :           char some_array_name [dim1] [dim2]
  15. : BUT, he said, if [...] we want a two-dimensional array that
  16. : we can pass around to/from functions (and what good is an array that
  17. : you couldn't?) we should define it thusly:
  18. :     typedef struct { char text[dim1][dim2]; } 2-d_arraytype;
  19. :     2-d_arraytype TheArray;
  20. : And from there access it as:
  21. :     TheArray.text[row][col]
  22. : Or pass it to a function as:
  23. :     void some_func (2-d_arraytype *a_pointer)
  24. : And, of course, from there access it as:
  25. :     *a_pointer->text[row][col] = some_character;
  26. : My real question is, then, Is this the "correct" way to manipulate
  27. : 2-d arrays, or is there a better way.
  28.  
  29. Get a new teacher.  Seriously.  Waste no more time with this one.
  30.  
  31. It is true that if you wish to pass arrays *by*value*, you must
  32. enclose them in a structure, because of the behavior of objects
  33. of array types in value contexts.  But the example listed above
  34. is much better handled by
  35.  
  36.         void f(int formal_array[N][M]){
  37.             ... formal_array[i][j] ...
  38.         }
  39.         void g(void){
  40.             int actual_array[N][M];
  41.             ... f(actual_array); ...
  42.         }
  43.  
  44. It is also true that if you wish to pass *adaptive* arrays in C,
  45. you either have to simulate multiple dimensional addressing, or
  46. use nonstandard extensions available in some compilers such as gcc.
  47.  
  48. Finally, the expression (*a_pointer->text[row][col]) above is
  49. ill considered: it won't even compile as given.  It has too
  50. many indirection operations.  The three C compilers I have here
  51. just now have this to say about it (when placed in a compilable
  52. context:
  53.  
  54.         1st compiler:   illegal indirection
  55.         2nd compiler:   invalid type argument of `unary *'
  56.         3rd compiler:   "a_pointer->text[1][1]" has a char type, but
  57.                         occurs in a context that requires a pointer
  58.  
  59. ( Well... the string "2-d_array_type" isn't a valid identifier in C...
  60.   I also substituted "two_d_array_type" as appropriate... )
  61. --
  62. Wayne Throop  ...!mcnc!dg-rtp!sheol!throopw
  63.