|
|
|
|
|
|
| Author |
Message |
G Patel *nix forums beginner
Joined: 23 Feb 2005
Posts: 30
|
Posted: Fri Jul 21, 2006 2:45 am Post subject:
nested switches, with common labels
|
|
|
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? |
|
| Back to top |
|
 |
G Patel *nix forums beginner
Joined: 23 Feb 2005
Posts: 30
|
Posted: Fri Jul 21, 2006 2:51 am Post subject:
Re: nested switches, with common labels
|
|
|
G Patel wrote:
| Quote: | 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. |
|
| Back to top |
|
 |
Morris Dovey *nix forums addict
Joined: 22 Jun 2005
Posts: 90
|
Posted: Fri Jul 21, 2006 3:47 am Post subject:
Re: nested switches, with common labels
|
|
|
G Patel (in 1153449952.792968.137910@p79g2000cwp.googlegroups.com)
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."
--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto |
|
| Back to top |
|
 |
Martin Ambuhl *nix forums Guru
Joined: 20 Feb 2005
Posts: 582
|
Posted: Fri Jul 21, 2006 6:08 am Post subject:
Re: nested switches, with common labels
|
|
|
G Patel wrote:
| Quote: | 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;
}
| Quote: | 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 */
} |
|
|
| Back to top |
|
 |
Google
|
|
| Back to top |
|
 |
|
|
The time now is Sun Nov 23, 2008 1:40 pm | All times are GMT
|
|
Loans | Mortgage | Agencia de turismo | Loans | Loan
|
|
Copyright © 2004-2005 DeniX Solutions SRL
|
|
|
|
Other DeniX Solutions sites:
Unix/Linux blog |
electronics forum |
medicine forum |
science forum |
|
|
Privacy Policy
|
Powered by phpBB © 2001, 2005 phpBB Group
|
|