Slimelite BBS

PASSING DIFFERENT SIZE AR

Back to EMULATE

From Walt Karas · 1996-03-29 03:17 · #73503 in EMULATE

TC>Hi All!
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]
TC>   void func (         )

TC>   What would be the function parameter in this case?

It can only be done cleanly if all but the first dimensions are
the same:

char a[K1][K2][K3];
char b[K4][K2][K3];
        .
        .
        .


void func(char (*p)[K2][K3], unsigned int first_dim)
        .
        .
        .


func(a, K1);
func(b, K4);

Otherwise, it required alot of ugly casting and index arithmetic:


char a[K1][K2][K3];
char b[K4][K5][K6];
        .
        .
        .

void func(char *p, unsigned int dim1, unsigned int dim2,
                   unsigned int dim3)
    {

    ... p[(((i1 * dim2) + i2) * dim3) + i3] ... // p[i1][i2][i3]

    }


func((char *) a, K1, K2, K3);
func((char *) b, K4, K5, K6);

In one of Stroustrup's books, he talks about the desirability of
using an "array" template to create an alternative definition
of arrays.  The above is an example of the kind of situations that
make standard arrays difficult to work with.
---
 þ SLMR 2.1a þ 
                                                                            

--- Simplex BBS (v1.07.00Beta [DOS])
 * Origin: To hell with the prime directive, FIRE! (1:151/117)