home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!psinntp!dg-rtp!sheol!throopw
- From: throopw@sheol.UUCP (Wayne Throop)
- Newsgroups: comp.lang.c
- Subject: 2-D Arrays
- Message-ID: <722575658@sheol.UUCP>
- Date: 24 Nov 92 00:32:53 GMT
- References: <1992Nov23.010230.45040@kuhub.cc.ukans.edu>
- Lines: 53
-
- : From: 2hujwant@kuhub.cc.ukans.edu
- : Message-ID: <1992Nov23.010230.45040@kuhub.cc.ukans.edu>
- : our teacher told us that you define them [..2-d arrays..]
- : just like any other array, but with another index. i.e.:
- : char some_array_name [dim1] [dim2]
- : BUT, he said, if [...] we want a two-dimensional array that
- : we can pass around to/from functions (and what good is an array that
- : you couldn't?) we should define it thusly:
- : typedef struct { char text[dim1][dim2]; } 2-d_arraytype;
- : 2-d_arraytype TheArray;
- : And from there access it as:
- : TheArray.text[row][col]
- : Or pass it to a function as:
- : void some_func (2-d_arraytype *a_pointer)
- : And, of course, from there access it as:
- : *a_pointer->text[row][col] = some_character;
- : My real question is, then, Is this the "correct" way to manipulate
- : 2-d arrays, or is there a better way.
-
- Get a new teacher. Seriously. Waste no more time with this one.
-
- It is true that if you wish to pass arrays *by*value*, you must
- enclose them in a structure, because of the behavior of objects
- of array types in value contexts. But the example listed above
- is much better handled by
-
- void f(int formal_array[N][M]){
- ... formal_array[i][j] ...
- }
- void g(void){
- int actual_array[N][M];
- ... f(actual_array); ...
- }
-
- It is also true that if you wish to pass *adaptive* arrays in C,
- you either have to simulate multiple dimensional addressing, or
- use nonstandard extensions available in some compilers such as gcc.
-
- Finally, the expression (*a_pointer->text[row][col]) above is
- ill considered: it won't even compile as given. It has too
- many indirection operations. The three C compilers I have here
- just now have this to say about it (when placed in a compilable
- context:
-
- 1st compiler: illegal indirection
- 2nd compiler: invalid type argument of `unary *'
- 3rd compiler: "a_pointer->text[1][1]" has a char type, but
- occurs in a context that requires a pointer
-
- ( Well... the string "2-d_array_type" isn't a valid identifier in C...
- I also substituted "two_d_array_type" as appropriate... )
- --
- Wayne Throop ...!mcnc!dg-rtp!sheol!throopw
-