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