home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #16 / NN_1992_16.iso / spool / comp / sys / acorn / 7961 < prev    next >
Encoding:
Internet Message Format  |  1992-07-20  |  2.9 KB

  1. From: kers@hplb.hpl.hp.com (Chris Dollin)
  2. Date: Tue, 21 Jul 1992 08:00:03 GMT
  3. Subject: Re: Passing N-dim array to a function in C?
  4. Message-ID: <KERS.92Jul21090003@cdollin.hpl.hp.com>
  5. Organization: Hewlett-Packard Laboratories, Bristol, UK.
  6. Path: sparky!uunet!wupost!sdd.hp.com!hpscdc!hplextra!otter.hpl.hp.com!hpltoad!cdollin!kers
  7. Newsgroups: comp.sys.acorn
  8. References: <1992Jul21.173107.9517@waikato.ac.nz>
  9. Sender: news@hplb.hpl.hp.com (Usenet News Administrator)
  10. Lines: 64
  11. In-Reply-To: mcg@waikato.ac.nz's message of 21 Jul 92 17:31:07 +1200
  12. Nntp-Posting-Host: cdollin.hpl.hp.com
  13.  
  14. In article ... mcg@waikato.ac.nz writes:
  15.  
  16.    A question for those C moguls out there:-
  17.  
  18.     One can declare a function
  19.             void matrix (double *m)
  20.             {
  21.         blah
  22.             }
  23.    and pass it an array declared : double t[10];
  24.  
  25.     matrix(t);
  26.  
  27.     What is the data_type declaration that's necessary for an array of an 
  28.     array? viz
  29.  
  30.        void  matrix(????)
  31.        {   double *p = malloc(sizeof (m));
  32.      m[1,4] = 1.0123;
  33.      p[3,9] = 1.20394;
  34.        ..}
  35.  
  36. First, be aware that C multi-dimensional arrays are just arrays of arrays, and
  37. that the notation to index them is thus  ``a[i][j]'', not ``a[i,j]'' [*1].
  38.  
  39. Second, to pass such an array as an argument:
  40.  
  41.     void flakjacket( double d[][10] ) { ... }
  42.  
  43. Note that you *must* specify the size of the second dimension of ``d'', but you
  44. need not specify the first. Also, C actually passes a *pointer*, not an array
  45. (as you correctly noted in your one-dimensional example), and silently rewrites
  46. the type of ``d'' as if you had declared it as 
  47.  
  48.     double (*d)[10]
  49.  
  50. ie, ``d'' is a pointer to (things which are) arrays of 10 doubles.
  51.  
  52. I hope this helps; without knowing what you're actually trying to *do*, I can't
  53. be more exact. (Feel free to mail; if anyone else wants to know the Answers,
  54. let me know, and I'll summarise.)
  55.  
  56.  
  57. [*1] The expression ``a[i,j]'' *is* legal C, but with an entirely inobvious
  58. effect (until you get used to C). ``e1, e2'' is a use of the ``comma
  59. operator'', which evaluates ``e1'', ignores any result, and then evaluates
  60. ``e2'', with its result being the result of the entire comma-expression [*2].
  61. Thus ``a[i,j]'' indexes ``a'' with ``j'' (delivering a single-dimensional
  62. array), which will almost certainly be in a position for The Rule [*3] to apply
  63. - the array decays into a pointer to its first element. This is probably going
  64. to be a poor substitute for the double you were expecting, and the compiler
  65. will probably mumble something at you.
  66.  
  67. [*2] Do *not* confuse the comma of the comma operator with the comma used to
  68. separate arguments to functions; the two are related only by appearance. C does
  69. not mandate any order on the evaluation of arguments to functions.
  70.  
  71. [*3] The term ``The Rule'' was coined by Chris Torek, comp.lang.c guru.
  72.  
  73.  
  74. --
  75.  
  76. Regards,    | It's strange how you suddenly discover bizarre and dangerous
  77. Kers.       | holes in your knowledge.  I fell down one today. - Steve Knight
  78.