Passing types to a subroutine
Chris Torek
chris at mimsy.UUCP
Fri Nov 11 12:09:39 AEST 1988
In article <32119 at bbn.COM> mesard at bbn.com (Wayne Mesard) writes:
>Or, how 'bout using a good old union to get rid of the extra param:
You can do this, but not as coded. (Original code appears below.)
Instead, you must augment main() with a `union ptr' variable, and
change the calls to
u.f = f;
sub(0, u);
u.p = d;
sub(1, d);
The code below is not type-correct and WILL NOT WORK on (eg) a Pyramid.
I would also suggest using TYPE_FLOAT and TYPE_DOUBLE rather than the
literal 0 and 1 constants.
- union ptr {
- float *f;
- double *d;
- };
-
- enum whichtype {TYPE_FLOAT, TYPE_DOUBLE};
-
- main()
- {
- void sub();
- float f[10];
- double d[10];
-
- f[1] = 1.234;
- d[1] = -5.432;
- sub(0, f);
- sub(1, d);
- printf("Incidently, the sizeof the union is %d\n", sizeof(union ptr));
- }
-
-
- void sub(t, p)
- enum whichtype t;
- union ptr p;
[rest deleted]
--
In-Real-Life: Chris Torek, Univ of MD Comp Sci Dept (+1 301 454 7163)
Domain: chris at mimsy.umd.edu Path: uunet!mimsy!chris
More information about the Comp.lang.c
mailing list