Must the "switch" quantity be an integer?

David Keppel pardo at june.cs.washington.edu
Thu Oct 20 05:23:25 AEST 1988


[ appologies if this was just discussed ]

I want to declare a bunch of "magic" pointers, e.g., as is done in
/usr/include/signal.h:

    #define	BUFPTR		((os_t *)1)
    #define	SPTR		((os_t *)2)
     :

And do things like:

    foo[0] = BUFPTR;
    foo[1] = (os_t *) malloc (sizeof(os_t));
     :

then:

    switch (foo[i]) {
	case BUFPTR:
	    /* handle magic case #1 */
	    break;
	case SPTR:
	    /* handle magic case #2 */
	    break;
	case ...
	 :
	 :
	default:
	    normal (foo[i]);
    }

When I compile this with pcc, everything is happy.
When I compile this with gcc and turn on all of the
flags for dpANS (-ansi, -pedantic), the compiler
refuses to accept the code since "switch quantity is
not an integer".  This *is* certainly true, but isn't
what I expected.  I wasn't aware that dpANS required
the switch quantity to be an integer (or, presumably
things such as chars that can be converted to integers
implicitly).

Two questions:
(a) What are the rules?
(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?")

	    ;-D on  ( Compilo ad adsurd-bum )  Pardo
-- 
		    pardo at cs.washington.edu
    {rutgers,cornell,ucsd,ubc-cs,tektronix}!uw-beaver!june!pardo



More information about the Comp.lang.c mailing list