Mail Archives: djgpp/1997/03/10/05:45:08
To store a rectangular two d array dynamically, you don't have to make an
array of MAX_Y pointers and calloc a MAX_X*sizeof(struct) for each. Just
do this:
MY_TYPE (*my_pointer)[MAX_Y];
my_pointer=calloc (MAX_X*MAX_Y*sizeof(MY_TYPE));
dereference and access an element of the 2D array with:
my_pointer[x][y]
(For 3D: MY_TYPE (*my_pointer)[MAX_Y][MAX_Z];
my_pointer=calloc(MAX_X*MAX_Y*MAX_Z*sizeof(MY_TYPE));
dereference: my_pointer[x][y][z])
In these cases, x going out of bounds will cause segmentation violation. y
and z going out of bounds will address the wrong x or y
coordinate respectively (or crash if it goes way way out of bounds).
--
.*. Where feelings are concerned, answers are rarely simple [GeneDeWeese]
-() < When I go to the theater, I always go straight to the "bag and mix"
`*' bulk candy section...because variety is the spice of life... [me]
Paul Derbyshire ao950 AT freenet DOT carleton DOT ca, http://chat.carleton.ca/~pderbysh
- Raw text -