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

  1. Path: sparky!uunet!spool.mu.edu!caen!kuhub.cc.ukans.edu!2hujwant
  2. From: 2hujwant@kuhub.cc.ukans.edu
  3. Newsgroups: comp.lang.c
  4. Subject: 2-D Arrays
  5. Message-ID: <1992Nov23.010230.45040@kuhub.cc.ukans.edu>
  6. Date: 23 Nov 92 01:02:29 CST
  7. Organization: University of Kansas Academic Computing Services
  8. Lines: 60
  9.  
  10.  
  11. OK. I have a question:
  12.  
  13.         How are two-dimensional arrays SUPPOSED to be defined?
  14.  
  15. When I took my "intro to C" class, our teacher told us that you 
  16. define them just like any other array, but with another index. 
  17. i.e.: 
  18.  
  19.         char some_array_name [dim1] [dim2]
  20.  
  21. and that would take care of allocating the memory. BUT, he said, if 
  22. we want to pass these arrays to a function, that the compiler, in 
  23. its non-range-checking way would not know how the array was defined 
  24. from just the pointer to a base address, (and I suppose this makes
  25. sense to me). So, he said, if we want a two-dimensional array that 
  26. we can pass around to/from functions (and what good is an array that 
  27. you couldn't?) we should define it thusly:
  28.  
  29.         typedef struct {
  30.                 char text[dim1][dim2];
  31.         } 2-d_arraytype;
  32.  
  33.         2-d_arraytype TheArray;
  34.  
  35. And from there access it as:
  36.  
  37.         TheArray.text[row][col]
  38.  
  39. Or pass it to a function as:
  40.  
  41.         void some_func (2-d_arraytype *a_pointer)
  42.  
  43. And, of course, from there access it as:
  44.  
  45.         *a_pointer->text[row][col] = some_character;
  46.  
  47. etc, etc, etc.
  48.  
  49. My real question is, then, Is this the "correct" way to manipulate 
  50. 2-d arrays, or is there a better way. The problem (for me) comes 
  51. when I try to pass that array pointer to an external assembly 
  52. routine. I don't think I understand exactly how that looks in 
  53. memory, which is what you really need to know to do any real 
  54. processing on it in assembly.
  55.  
  56. So, if any of you veteran coders out there want to take the time to 
  57. e-mail me a response to this, I would be very grateful. Also, if 
  58. you do, I have plenty of other small/petty/novice-type questions 
  59. that I could post, as long as no one gets too annoyed.
  60.  
  61. Later!
  62.  
  63. ====================================================================
  64. Sean White                                              
  65.                                                          
  66. an Outlaw at                                              
  67. The University of Kansas -- NCAA champs 1988 and 1993!    
  68.                                                           
  69. ===================== 2hujwant@kuhub.cc.ukans.edu ==================
  70.