Passing types to a subroutine

Wade Guthrie evil at arcturus.UUCP
Sat Nov 12 04:24:25 AEST 1988


In article <14461 at mimsy.UUCP>, chris at mimsy.UUCP (Chris Torek) writes:
> In article <14457 at mimsy.UUCP> sjr at mimsy.UUCP (Stephen J. Roznowski) writes:
> >I'm in the process of developing a piece of code that needs
> >to be portable across several different machines.  What I
> >need to do is something of the following:
> [edited]
> >	subroutine(...., <float array>, ...);
> >	subroutine(...., <double array>, ...);
> >
> This is easy: you cannot do it at all in C.

how about using unions -- something like

union types
{
	float *f;
	double *d;
};

#define FLOAT	1
#define DOUBLE	2

subroutine(...,fred, type);
union *fred;
int type;
{
	. . .
	switch(type)
	{
		case(FLOAT):
			. . . fred->f . . .
			break; 
		case(DOUBLE):
			. . . fred->d . . .
			break; 
		default:
			/* error */
	}
	. . .
}

It's a little cumbersome, but it gets you there -- I used pointers
to floats and doubles because, in most cases, they act like arrays.
I've used things like this and it seems to work okay.


Wade Guthrie
Rockwell International
Anaheim, CA

(Rockwell doesn't necessarily believe / stand by what I'm saying; how could
they when *I* don't even know what I'm talking about???)



More information about the Comp.lang.c mailing list