switch (expression)

Bakul Shah bvs at light.uucp
Fri Jul 15 13:58:32 AEST 1988


In article <253 at chem.ucsd.EDU> tps at 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.



More information about the Comp.std.c mailing list