aitken@svax.cs.cornell.edu (William Aitken) (06/14/88)
Yesterday, I was compiling some code and had a piece of code that I didn't think could possibly compile compile without error. In fact, lint didn't even complain. The problem is that the compiler allows one to use variables of struct type as switch selectors. This can't be right. Can it??? The reason I don't think its right is that there is no way to specify the case labels -- after all there are no anonymous structure constants in C. Furthermore, some quick investigation suggests that what the compiler does is to select on the first element of the structure. Below is a code fragment which compiles without error on SUN OS version 3.5. (incidentally Mt. Xinu 4.3 complains about it) struct splat { int foo_foo; int foo_bar; } foo; unsigned int convfoo(bar) struct splat bar; { switch (bar) { case 1:break; default:foo = bar;break; } return 0; } The question is this a bug, or this behavior mandated by K&R or the proposed ANSI standard. If the latter, why is such a kludge mandated? --- Bill. William E. Aitken <aitken@svax.cs.cornell.edu> (607)257-2542(h) {uw-beaver,ihnp4,vax135,decvax}!cornell!aitken (607)255-4222(o) aitken@crnlcs.BITNET 700 Warren Rd. #20-2A, Ithaca, NY 42 26'30" N 76 29'00" W 4148 Upson Hall
guy@gorodish.Sun.COM (Guy Harris) (06/14/88)
> The problem is that the compiler allows one to use variables of struct type > as switch selectors. This can't be right. Can it??? No, it can't. > Below is a code fragment which compiles without error on SUN OS version 3.5. "Fixed in 4.0". Some versions of PCC didn't catch this; 4.3BSD's and SunOS 4.0's versions have been fixed to do so (although 4.3BSD's just issues a warning, for some unknown reason; ours considers this an error).