Path: utzoo!utgpu!water!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!mailrus!ames!haven!ncifcrf!nlm-mcs!adm!smoke!gwyn From: gwyn@smoke.BRL.MIL (Doug Gwyn ) Newsgroups: comp.lang.c Subject: Re: Must the "switch" quantity be an integer? Message-ID: <8706@smoke.BRL.MIL> Date: 19 Oct 88 23:18:03 GMT References: <6137@june.cs.washington.edu> Reply-To: gwyn@brl.arpa (Doug Gwyn (VLD/VMB) ) Organization: Ballistic Research Lab (BRL), APG, MD. Lines: 29 In article <6137@june.cs.washington.edu> pardo@cs.washington.edu (David Keppel) writes: > #define BUFPTR ((os_t *)1) This is quite non-portable. ANSI C requires that there be some integral type to which a pointer can be cast then uncast without loss of information, but the integral type isn't necessarily int, and random integer values need not be castable to pointers. If you really have to have os_t*-shaped magic numbers, consider using real data objects for them: static os_t dummy_buf; #define BUFPTR (&dummy_buf) >(a) What are the rules? The switch expression must be an integral expression. >(b) Can I do this portably without using a mass of > if (foo[i]==BUFPTR) { ... } else if (foo[i]==SPTR)... > code? (Or, "*can* I do this portably?") One way, assuming that you use actual objects as I suggested above and that ptrint_t is the integral type defined for pointer-to-integer mapping for your implementation (the typedef needs to be adjusted when porting): switch ( (ptrint_t)foo[i] ) { case (ptrint_t)BUFPTR: