Slimelite BBS

PASSING DIFFERENT SIZE AR

Back to EMULATE

From Michael Blischke · 1996-03-31 14:04 · #73506 in EMULATE

TC>   is it possible to have one function, which can accept different sizes
TC>   of mulit-dimenstuonal arrays, ie.

TC>   char a [20] [10]
TC>   char b [20] [20]
TC>   char b [20] [30]

Get a hold of (or preferably GET) Numerical Recipes by Press,
Teukolsky, Vetterling and Flannery.  In chapter 1.2, they describe
exactly how to do this (and use it throghout the book).  Appendix B
gives all the code you'll need to do this, and explictly releases it
into the public domain.

Essentially what they do is create an auxilliary array, with elements
which are pointers to rows of the actual array.  The auxilliary array
is used just like a regular 2-d array.

The critical pieces of code are (somewhat modified.  You should really
still see the book):

float **m=(float **) malloc((size_t)((nr+1)*sizeof(float*)));
m[0]=(float *) malloc((size_t)((nr+1)*(nc+1)*sizeof(float)));
for(i=1;i<=nr+1;i++) m[i]=m[i-1]+nc+1;

This gives a 2-d array m[nr][nc].  Line 1 allocates the auxilliary
array, line 2 allocates the memory for the 2-d array, and line 3 gets
the pointers of the aux. array set up right.

You then will pass m into the function, along with its dimensions.

Michael

 * KWQ/2 1.2i * "The afterlife is banal too."   - Tom Edwards

--- WILDMAIL!/WC v4.12 
 * Origin: Jetstream BBS þ Fremont, CA þ (510) 252-9131  (1:215/69.0)