Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.2 9/18/84; site codas.UUCP Path: utzoo!watmath!clyde!burl!ulysses!mhuxr!mhuxt!houxm!whuxl!whuxlm!akgua!akguc!codas!mikel From: mikel@codas.UUCP (Mikel Manitius) Newsgroups: net.lang.c Subject: Re: Passing Multidimensional Arrays Message-ID: <397@codas.UUCP> Date: Tue, 17-Dec-85 23:36:30 EST Article-I.D.: codas.397 Posted: Tue Dec 17 23:36:30 1985 Date-Received: Fri, 20-Dec-85 01:14:05 EST References: <138@brl-tgr.ARPA> <365@codas.UUCP> <34@dg_rtp.UUCP> Organization: AT&T Information Systems (SDSS) - Orlando Lines: 72 > > > I have an application where I want to pass a 2D array to a subroutine. > > > The data does not easily avail itself to being converted to a 1D array > > > of structures. How can one access the array in the subroutine??? > > > How about this: > > #define X 100 > > #define Y 100 > > struct x a[Y][X]; > > subrout(i, j, a) > > int i; > > int j; > > struct x *a[]; > > { ...a[j][i]... } > > And use i and j as boundary limits. > > Mikel Manitius ...{ihnp4|akguc|attmail|indra!koura}!codas!mikel > > The main problem with this is that (although it is a legal C fragment), > it shows neither the definition of x, nor an invocation of subrout. > Thus, there is no clear notion as to what is supposed to be passed to > the formal arguments i, j, and a. Also, the formal "a" hides the actual > > Now, I may have guessed wrongly about what Mikel intended. But I think > it is clear that the example he posted is dangerously incomplete. I > think it is also clear that the best available tools should be used to > check out code fragments that are posted as examples for others to use. > Without actually trying it out, I couldn't (without a fair amount of > thought, vulnerable to error) have put my finger on exactly what was > wrong with the fragment Mikel posted. > -- > Wayne Throop at Data General, RTP, NC > !mcnc!rti-sel!dg_rtp!throopw Alas, your guesses were correct. Then explain, if you don't mind, how the compiler knows the dimensions of argv in the following construct: -------- #include main(argc, argv) int argc; char *argv[]; { int i, j; for(j=0;argv[j][0] != '\0';j++) { for(i=0;argv[j][i] != '\0';i++) putc(argv[j][i], stdout); putc('\n', stdout); } } ------- The preceeding program should print it's arguments, one per line, it is done in this manner to show that the compiler does not need to know the dimension of argv at compile time, in order to access the elements of the (2D) array, given i and j. The result is: ------- $ a.out one two twee a.out one two twee $ ------- panic: data allignment error -- Mikel Manitius @ AT&T-IS Altamonte Springs, FL ...{ihnp4|akgua|bellcore|clyde|koura}!codas!mikel