nested switches, with common labels

G

G Patel

Compiler won't let me nest switch statements with common labels inside
them (in the cases). Why is this a problem (either in terms of
standard or in terms of compiler's ability to make sense of it)?


ex:

typedef enum {ONE, TWO, THREE} TYPE; /* dumb use, but explains
situation */

TYPE var1, var2;

/* code that sets var1,var2 somehow */

switch(var1)
{
case ONE: /* code */

case TWO: /* code */
switch(var2)
{
case: ONE: /* code -- ! 1st complain ! */

case: TWO: /* code */

}

case THREE: /* code */
}




===========

compiler complains first about the ONE label being a "duplicate label"
and "previously defined here"



What's the reason for this?
 
G

G Patel

G said:
Compiler won't let me nest switch statements with common labels inside
them (in the cases). Why is this a problem (either in terms of
standard or in terms of compiler's ability to make sense of it)?


ex:

typedef enum {ONE, TWO, THREE} TYPE; /* dumb use, but explains
situation */

TYPE var1, var2;

/* code that sets var1,var2 somehow */

switch(var1)
{
case ONE: /* code */

case TWO: /* code */
switch(var2)
{
case: ONE: /* code -- ! 1st complain ! */

case: TWO: /* code */

}

case THREE: /* code */
}




===========

compiler complains first about the ONE label being a "duplicate label"
and "previously defined here"



What's the reason for this?


Nevermind. When I made up the example for this thread, I did the
coding properly. Which is funny, because my actual code didn't have
the "case" statements.

I appologize for polluting clc with my mistake.
 
M

Morris Dovey

G Patel (in (e-mail address removed))
said:

| Compiler won't let me nest switch statements with common labels
| inside them (in the cases). Why is this a problem (either in terms
| of standard or in terms of compiler's ability to make sense of it)?
|
| ex:
|
| typedef enum {ONE, TWO, THREE} TYPE; /* dumb use, but explains
| situation */
|
| TYPE var1, var2;
|
| /* code that sets var1,var2 somehow */
|
| switch(var1)
| {
| case ONE: /* code */
|
| case TWO: /* code */
| switch(var2)
| {
| case: ONE: /* code -- ! 1st complain ! */
|
| case: TWO: /* code */
|
| }
|
| case THREE: /* code */
| }
|
| ===========
|
| compiler complains first about the ONE label being a "duplicate
| label" and "previously defined here"
|
| What's the reason for this?

Sounds suspicious to me. The standard [in 6.8.4.2(3)] would seem to
allow this:

"Any enclosed switch statement may have a *default* label or *case*
constant expressions with values that duplicate *case* constant
expressions in the enclosing *switch* statement."
 
M

Martin Ambuhl

G said:
Compiler won't let me nest switch statements with common labels inside
them (in the cases). Why is this a problem (either in terms of
standard or in terms of compiler's ability to make sense of it)?

The problem is the bogus colons after the keyword 'case'. Notice that
the code below (without the bogus colons and as a compilable translation
unit) will compile and run just fine:

#include <stdio.h>

typedef enum
{ ONE, TWO, THREE } TYPE;

int main(void)
{
TYPE var1 = ONE, var2 = TWO;

switch (var1) {
case ONE:
puts("var1 is ONE");
break;

case TWO:
puts("var1 is TWO");
switch (var2) {
case ONE:
puts("var2 is ONE");
break;

case TWO:
puts("var2 is TWO");
break;
default:
puts("var2 is neither ONE nor TWO");
break;
}

case THREE:
puts("var1 is THREE");
break;
default:
puts("var1 is not ONE, TWO, or THREE");
break;
}
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,216
Latest member
topweb3twitterchannels

Latest Threads

Top