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

  1. Path: sparky!uunet!spool.mu.edu!umn.edu!csus.edu!netcom.com!netcomsv!spacebbs!ted.jensen
  2. From: ted.jensen@spacebbs.com (Ted Jensen) 
  3. Newsgroups: comp.lang.c
  4. Subject: 2-D Arrays, Revisited.
  5. Message-ID: <14297.610.uupcb@spacebbs.com>
  6. Date: 19 Nov 92 06:13:00 GMT
  7. Distribution: world
  8. Organization: SPACE BBS - Menlo Park, CA - 10 Lines + 4gB - 415-323-4193
  9. Reply-To: ted.jensen@spacebbs.com (Ted Jensen) 
  10. Lines: 90
  11.  
  12.  
  13. In Message-ID: <17489#pitt.UUCP> fahad@cs.pitt.edu (Fahad A Hoymany)
  14. writes:
  15.  
  16. > I am writing a function that should accept a 2-D array (hopefully
  17. > of any size), operate on that array, and returns 'anything' to
  18. > the caller. Please don't suggest I use double pointers because I
  19. > do not want to restrict the users of my function to use them when
  20. > they are used to declaring arrays as : int a[2][5].
  21.  
  22.  
  23. int your_function(int a[][5]);
  24.  
  25. I know you are not going to like this, but you are going to have
  26. to include the value of the second dimension, though this might
  27. be done with a define as in:
  28.  
  29. #define ROWS 2
  30. #define COLS 5
  31.  
  32. int a[ROWS][COLS];
  33.  
  34. int your_function(int a[][COLS]);       /* function prototype */
  35.  
  36. The reason is simple enough.  When you have a two dimensional
  37. array as a parameter the compiler treats it as a pointer and
  38. within your_function when you write:
  39.  
  40.     a[1][3] = 17;
  41.  
  42. the compile must compile this as if it were written:
  43.  
  44.     *(a + COLS*1 +3) = 17;
  45.  
  46. which it cannot do without knowing the value of the second
  47. dimension, COLS.  Thus were you to write the function in a
  48. seperate file, one which did not contain the definition of a as
  49. int a[ROWS][COLS], the compiler would not know what to do.
  50.  
  51. If you want a function which will handle any 2 dimensional array
  52. you might try passing an integer pointer and an integer such as
  53. in the following code:
  54.  
  55. ------------------------------------------------------
  56. #include <stdio.h>
  57.  
  58. void my_func(int *a, int ncols);
  59.  
  60. int arrayA[5][12];
  61. int arrayB[7][21];
  62.  
  63. void main(void)
  64. {
  65.   printf("\n arrayA initial value = %d\n",arrayA[2][3]);
  66.   my_func(arrayA,12);
  67.   printf("arrayA modified value = %d\n",arrayA[2][3]);
  68.   printf("arrayB initial value = %d\n",arrayB[2][3]);
  69.   my_func(arrayB,21);
  70.   printf("arrayB modified value = %d\n",arrayB[2][3]);
  71. }
  72.  
  73.  
  74. void my_func(int *a, int ncols)
  75. {
  76.   *(a + ncols*2 + 3) = 17;
  77. }
  78. -----------------------------------------
  79. Note that you must use the pointer notation instead of array
  80. notation within your function.  Since there is no need for your
  81. user to see the internals of your function this should not be
  82. a problem.  You could also pass the function the number of rows
  83. as well as the number of cols which might prove useful depending
  84. on what the function is doing.
  85.  
  86. The above gives a Suspicious pointer conversion warning on the
  87. two calls to my_func() in main() simply because arrayA and arrayB
  88. are _not_ simple integer pointers.  The warning can be avoided by
  89. using    my_func((int *)arrayA,12);   as the call instead of what
  90. is shown.  Either the cast or the warning may bother you user so
  91. you should be aware of that.
  92.  
  93. This is my first thought on this problem, so it may not be the
  94. most elegant solution.  If others do better I would appreciate
  95. the opprtunity to look at their approaches.
  96.  
  97. Hope this helps!  Ted Jensen  Redwood City, Calif.
  98.  
  99.  
  100.  * SLMR 2.1a * 
  101.                                                             
  102.