Passing Multidimensional Arrays

Mikel Manitius mikel at codas.UUCP
Wed Dec 18 14:36:30 AEST 1985


> > > 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
> <the-known-world>!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 <stdio.h>

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



More information about the Comp.lang.c mailing list