473,473 Members | 1,891 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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?

Jul 21 '06 #1
3 1512

G Patel wrote:
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.

Jul 21 '06 #2
G Patel (in 11**********************@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
Jul 21 '06 #3
G Patel wrote:
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;
}

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 */
}
Jul 21 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

36
by: invni | last post by:
I have a nested while. How do I go from the inner while to the beginning of the outer while? Can this be done without using goto? while_1() { some codes here while_2() { if true go to the...
2
by: Mike W | last post by:
I'm trying to programmatically run msinfo32.exe /? with the following line of code but it is not taking the switch: System.Diagnostics.Process.Start("c:\program files\common files\Microsoft...
77
by: Peter Olcott | last post by:
http://www.tommti-systems.de/go.html?http://www.tommti-systems.de/main-Dateien/reviews/languages/benchmarks.html The above link shows that C# is 450% slower on something as simple as a nested loop....
6
by: Polomora | last post by:
Hello. I've done a bit of searching here on c.l.c, and I also checked out SMC at soureforge, but haven't been able to find what I'l looking for. I've got two FSMs: A and B, where I want to...
25
by: GY2 | last post by:
I writing some documentation and I want to describe a common code structure which is used to step through all the items in a collection (e.g. each file in a subdirectory) while applying more and...
18
by: desktop | last post by:
I have 3 types of objects: bob1, bob2 and bob3. Each object is identified by a unique ID which gets returned by the function getId(). All bobs are descendants from class BaseBob which is an...
1
by: andrew.douglas11 | last post by:
Hello all, Is it possible to share controls inside a child repeater? Here's the situation: I have a web page that needs to display data grouped by A, and alternatively by B (only one grouping...
18
by: MZaza | last post by:
Hello, When I compile it I get an error in line 16, which got case 'c'. #include <cstdlib> #include <iostream> #include <math.h> using namespace std; int main()
13
by: JRough | last post by:
I got lost on formatting the nested if/else's on the bottom of the file right up the last else/if there seems to be a stray bracket. I'm trying to line up the brackets so I can read it. Is it...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.