array assignment

del Amitri del at AB.ECN.PURDUE.EDU
Fri May 4 04:44:30 AEST 1990


no luck in comp.lang.c.

from within a teeney-little routine (func()), i need to assign values
to some arrays, whose address and size i pass to that array.

the actual assignemnt syntax within func() has me baffled...

thanks
del at ab.ecn.purdue.edu
-------------------------------------------------------------------------------

the array types are declared by the union:

union	utyp {
	int	*_int;
	float	*_float;
	char	**_char;
}

#define	_INT	1
#define	_FLT	2
#define	_ALP	3

so:
main()
{
	float	a[10][3];
	char	b[10][3][20];
	int	c[10][3];

	func(a, _FLT, 10, 3);
	func(b, _ALP, 10, 3);
	func(c, _INT, 10, 3);
}

func(var, typ, ysize, xsize)
union	utyp	*var;
int	typ, ysize, xsize;
{
	if (!ysize) ysize = 1;
	if (!xsize) xsize = 1;

	for (i = 0 ; i < ysize ; i++)
	for (j = 0 ; j < xsize ; j++)
		switch (typ) {
			/*
				depending upon the incoming array type,
				assign values into that incoming array
				(essentially need to do x[i][j] = value)
				the values being assigned are globally
				accessible.
			*/
			/*
				so, what is the syntax for each case?
			*/
			case _INT:  var->_int[i * xsize + j] = value;
			case _FLT:  var->_float[i * xsize + j] = value;
			case _ALP:  strcpy(var->_char[i * xsize + j],
					value);
		}
}



More information about the Comp.unix.wizards mailing list