Passing types to a subroutine

Henry Spencer henry at utzoo.uucp
Sat Nov 12 04:37:10 AEST 1988


In article <14457 at mimsy.UUCP> sjr at mimsy.UUCP (Stephen J. Roznowski) writes:
>	float	a[100];
>	subroutine(...., a[100], ...);
>
>	double	a[100];
>	subroutine(...., a[100], ...);
>
>and have the subroutine do the right thing with a[].
>How do you pass the knowledge of the array type to the
>subroutine?  I need to be able to pass the knowledge
>that the array a[] is either float or double (or for
>that matter unsign int).

You can't do this in C.  A function parameter has to have a single, specific
type at compile time; you don't get to postpone the decision to run time.
The best you can do is a complicated little dance with unions and pointers,
which isn't really any simpler than just writing three versions of your 
subroutine, one for each type.
-- 
Sendmail is a bug,             |     Henry Spencer at U of Toronto Zoology
not a feature.                 | uunet!attcan!utzoo!henry henry at zoo.toronto.edu



More information about the Comp.lang.c mailing list