dynamically define 2-D array

Ron Watkins ron at argus.lpl.arizona.edu
Thu Nov 1 01:43:47 AEST 1990


If your question is how to declare such a beastie, try this on for size: (please
note that im just typing this in from my head not from some existing piece of
code);

int	i;
DTYPE	** foo;
...
	foo = (DTYPE **) malloc (NAXIS2 * sizeof (DTYPE *));
	for (i = 0; i < NAXIS2; i++)
	    foo[i] = (DTYPE *) malloc (NAXIS1 * sizeof (DTYPE));
/* OR */
	    * (foo + i) = (DTYPE *) malloc (NAXIS1 * sizeof (DTYPE));

Where:
1) DTYPE is your data type of interest. If you want multiple data type
capability, good luck. Maybe someone would be kind enough to enlighten both
of us on that one.
2) NAXIS1 is the fastest axis (note that you can't go zipping through this
two dimensional space with a pointer dereference like when you normally declare
2-D arrays).
3) NAXIS2 is the slower axis (presumably y?).
I supplied to different approaches for the body of the for loop. Please don't
use BOTH of them. :-)

P.S. If I have totally screwed this thing up, no flames please. Just nicely
tell me that I should go crawl under a rock! Oh and while your doing that,
please do send a proper response to the question at hand.

			_		PEACE!
		      / | \		Ron Watkins
		    /   |   \		ron at argus.lpl.arizona.edu
		   (   /|\   )
		    \ / | \ /
		      \ | /



More information about the Comp.lang.c mailing list