Path: utzoo!attcan!uunet!lll-winken!lll-tis!helios.ee.lbl.gov!pasteur!ames!amdcad!light!bvs From: bvs@light.uucp (Bakul Shah) Newsgroups: comp.std.c Subject: Re: switch (expression) Message-ID: <1988Jul14.205835.18871@light.uucp> Date: 15 Jul 88 03:58:32 GMT References: <1988Jul12.105547.13268@light.uucp> <253@chem.ucsd.EDU> Reply-To: bvs@light.UUCP (Bakul Shah) Organization: dis Lines: 34 In article <253@chem.ucsd.EDU> tps@chem.ucsd.edu (Tom Stockfisch) writes: >The problem is not the ptr expression, it's the PTR* constants. >In the original >K&R, this was disallowed. There are different classes of constants in C -- Use of link time constants as case expression would be nice, but I was only thinking of true ptr constants, such as (int_f)0x1234 or (int_f)1 where int_f is a ptr to a function returning int. Lookup definition of SIG_DFL etc in signal.h for an example. kern_sig.c in 4.3 BSD uses ``switch (u->u_signal)'' (I don't recall what V7 did -- probably the cast was done somewhere else). I mention the 4.3 use to merely point out that old PCC compilers accept switch (ptr). As long as one can create true constants for use as case labels, any object that can be compared for equality should be legal in switch(expr). I see not allowing ptrs in switch(expr) as an unnecessary restriction. Relaxing it will legitimize what many compilers already do. More importantly, `switch(expr) { case ... }' is easier to read/write than a string of `if .. then else if ...'. If the main function of a language is to help simplify programming, it should _do_ so; especially in this case where the benefit comes free. -- Bakul Shah <..!{ucbvax,sun,uunet}!amdcad!light!bvs> PS: casting a ptr back to a long is not safe because on some machines ptrs are longer than 4 bytes and long ints are not, so you'd lose some information.