c types problem
Joseph S. D. Yao
jsdy at hadron.UUCP
Mon Jan 6 03:45:00 AEST 1986
In article <870 at kuling.UUCP> gostas at kuling.UUCP (G|sta Simil{/ml) writes:
>The formal (but ugly) way to solve this would proparbly be to use a union
>for all (about 10) types. But now I wonder if anyone have a simpler but
>resonably portable solution?
I really don't think you'll find any more portable solution than to
use a union, which really isn't all that ugly if you declare the union
elsewhere and use some good macros. E.g.:
union mix {
int mix_i;
long int mix_li;
dev_t mix_d; /* Be sure to include dev_t and */
time_t mix_t; /* time_t et al: they may surprise */
/* you. */
...
};
union mix getval(int);
#define getint(x) (getval((x)).mix_i)
#define getlong(x) (getval((x)).mix_li)
...
If you're worried about portability, you certainly won't have any
places where you don't know the type of the return value. Oh: to
return a value, in the function you really should (e.g.):
union mix getval(x)
int x;
{
union mix retval;
...
retval.mix_i = Xxx;
return(retval);
...
}
which can be #define'd:
#define retmix(val,type) retval.type = val; return(retval);
=> retmix(Xxx, mix_i);
--
Joe Yao hadron!jsdy at seismo.{CSS.GOV,ARPA,UUCP}
More information about the Comp.lang.c
mailing list