Declarations in switches, errors

Wm E Davidsen Jr davidsen at crdos1.crd.ge.COM
Thu Sep 28 01:42:15 AEST 1989


In the process of looking for a totally diferent problem, I generated
the following program:

main() {
 int i = 2;
 switch (i) {
  int j = 4;
 case 1: j += 4; break;
 case 2:
 case 3:
 case 'a':
 case 'b':
  j--; break;
 }
}

Note the initialization in the 4th statement. I was unable to find any
compiler which generated working code for this initialization (although
one did complain that the code was not reached). I tried Sun, Ultrix,
Xenix and gcc compilers.

This is an error in the compilers! As ugly as this is, the ANSI standard
(3.1.2.4) says "If an initialization is specified for the value stored
in the object, it is performed on each normal entry, but not if the
block is entered by a jump to a label."

I submit that executing a switch statement constitutes "normal entry"
and that the initialization should be performed before evaluating the
switch variable. Would any of the people still on X3J11 like to comment?

Please let's not discuss the merits (or beauty) of this technique.
Although I was in an attempt to find another compiler error, I can
think of a few places where functional initialization would actually be
useful.

-- 
bill davidsen	(davidsen at crdos1.crd.GE.COM -or- uunet!crdgw1!crdos1!davidsen)
"The world is filled with fools. They blindly follow their so-called
'reason' in the face of the church and common sense. Any fool can see
that the world is flat!" - anon



More information about the Comp.std.c mailing list