delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/03/10/05:45:08

From: ao950 AT FreeNet DOT Carleton DOT CA (Paul Derbyshire)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: DJGPP Structs
Date: 10 Mar 1997 05:22:54 GMT
Organization: The National Capital FreeNet
Lines: 29
Message-ID: <5g05re$qaj@freenet-news.carleton.ca>
References: <01bc2673$a4dbe420$8e54dec2 AT satan> <3321EA8A DOT 6F3B3CC3 AT alcyone DOT com>
Reply-To: ao950 AT FreeNet DOT Carleton DOT CA (Paul Derbyshire)
NNTP-Posting-Host: freenet2.carleton.ca
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp


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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019