[comp.lang.c] Why is switch

dzoey@umd5.umd.edu (Joe Herman) (12/16/88)

If I have a structure tag:

typdef struct foo {
   int key;
   int pitch;
} NOTE;

and I declare the following structures:
NOTE do, re, mi;

and the pointer:
NOTE *scale;

Why can I not do:

	switch (scale) {

	case &do:
		do_something (scale);
		break;

	case &re:
		do_somethingelse (scale);
		break

  	}

etc.

The compiler I used says that scale isn't a scaler and that do, mi and re
are address expressions and can't be used in this context.  As a test,
I casted scale to an int and it stopped complaining about scale, but still
complained about the case values even thought they evaluate to constant
values (I'm not sure this matters).

So, why is it important that scale be a scaler?

			Joe Herman

dzoey@terminus.umd.edu
-- 
"Everything is wonderful until you know something about it."

mat@mole-end.UUCP (Mark A Terribile) (12/18/88)

 | NOTE do, re, mi;
 | 
 | and the pointer:
 | NOTE *scale;
 | 
 | Why can I not do:
 | 
 | 	switch (scale) {
 | 
 | 	case &do:
 | 		do_something (scale);
 | 		break;

The switch() requires *compile-time* constants, rather than link-time constants
so that the compiler can determine which of several methods (table look-up,
range-checked indexing, hashed back-checked lookup, if/then ...) it should use.

Could it be changed to always do a linear search table lookup (worst case) on
a link-time expression?  Probably.  Would it encourage bad style?  Perhaps.
Should it be done?  I don't know.
-- 

(This man's opinions are his own.)
From mole-end				Mark Terribile