Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP Posting-Version: version B 2.10.3 4.3bsd-beta 6/6/85; site hadron.UUCP Path: utzoo!linus!decvax!genrad!panda!talcott!harvard!seismo!rlgvax!hadron!jsdy From: jsdy@hadron.UUCP (Joseph S. D. Yao) Newsgroups: net.lang.c Subject: Re: c types problem Message-ID: <168@hadron.UUCP> Date: Sun, 5-Jan-86 12:45:00 EST Article-I.D.: hadron.168 Posted: Sun Jan 5 12:45:00 1986 Date-Received: Tue, 7-Jan-86 06:53:08 EST References: <870@kuling.UUCP> Reply-To: jsdy@hadron.UUCP (Joseph S. D. Yao) Organization: Hadron, Inc., Fairfax, VA Lines: 38 In article <870@kuling.UUCP> gostas@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@seismo.{CSS.GOV,ARPA,UUCP}