niXforums Forum Index
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   PreferencesPreferences   Log in to check your private messagesLog in to check your private messages   Log inLog in 
·  nixdoc.net ·  man pages ·  Linux HOWTOs ·  FreeBSD Tips ·  Forums
navigation Forum index » Programming » C
nested switches, with common labels
Post new topic   Reply to topic Page 1 of 1 [4 Posts] View previous topic :: View next topic
Author Message
G Patel
*nix forums beginner


Joined: 23 Feb 2005
Posts: 30

PostPosted: Fri Jul 21, 2006 2:45 am    Post subject: nested switches, with common labels Reply with 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?
Back to top
G Patel
*nix forums beginner


Joined: 23 Feb 2005
Posts: 30

PostPosted: Fri Jul 21, 2006 2:51 am    Post subject: Re: nested switches, with common labels Reply with quote

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

PostPosted: Fri Jul 21, 2006 3:47 am    Post subject: Re: nested switches, with common labels Reply with quote

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

PostPosted: Fri Jul 21, 2006 6:08 am    Post subject: Re: nested switches, with common labels Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic Page 1 of 1 [4 Posts] View previous topic :: View next topic
The time now is Sun Nov 23, 2008 1:40 pm | All times are GMT
navigation Forum index » Programming » C
Jump to:  

Similar Topics
Topic Author Forum Replies Last Post
No new posts Help with tied/nested data structures Clint Olsen Perl 13 Wed Jul 19, 2006 12:50 am
No new posts Return Nested XML from Heirarchical Query Mark Petereit Oracle 1 Mon Jul 17, 2006 5:19 pm
No new posts nested rested classes? Len Conrad Postfix 1 Sun Jul 16, 2006 12:05 am
No new posts Text delimiters getting stripped in nested script wheresjim shell 3 Thu Jul 06, 2006 6:07 pm
No new posts nested class JayEs C++ 4 Wed Jul 05, 2006 11:13 pm

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
[ Time: 0.1779s ][ Queries: 16 (0.0913s) ][ GZIP on - Debug on ]