473,396 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Defining a variable in switch statement

Hi,
Is the following correct? Here i have declaread a char array in case 1:
and am using the same in case 2:
first I tried another definition of ch in case 2: assuming that if i=2
then char ch[] will not get allocated in case 1:. But the compiler gave
me (re-definition error) so i removed it and now its working fine.
Is it correct as per the standard?

switch (i)
{
case 1:
char ch[12];
strcpy(ch, "case 1\n");
cout<<ch;
break;
case 2:
strcpy(ch, "case 2\n"); //Can i use it here, when i=2, why
should the compiler allocate
// the variable shich is
declared in the case which is not true.
cout<<ch;
break;

default:
strcpy(ch, "default\n"); // Is it ok here also?
cout<<ch;
}

Jul 12 '06 #1
11 1899
You can use { and } to meet your requirement.

[example]

switch ( i )
{
case 1:
{
char ch[1];
//...
break;
}
case 2:
{
char ch[2];
//...
break;
}
//...
}

[end example]

Rahul wrote:
Hi,
Is the following correct? Here i have declaread a char array in case 1:
and am using the same in case 2:
first I tried another definition of ch in case 2: assuming that if i=2
then char ch[] will not get allocated in case 1:. But the compiler gave
me (re-definition error) so i removed it and now its working fine.
Is it correct as per the standard?

switch (i)
{
case 1:
char ch[12];
strcpy(ch, "case 1\n");
cout<<ch;
break;
case 2:
strcpy(ch, "case 2\n"); //Can i use it here, when i=2, why
should the compiler allocate
// the variable shich is
declared in the case which is not true.
cout<<ch;
break;

default:
strcpy(ch, "default\n"); // Is it ok here also?
cout<<ch;
}
Jul 12 '06 #2

Forest wrote:
You can use { and } to meet your requirement.

[example]

switch ( i )
{
case 1:
{
char ch[1];
//...
break;
}
case 2:
{
char ch[2];
//...
break;
}
//...
}

[end example]

Rahul wrote:
Hi,
Is the following correct? Here i have declaread a char array in case 1:
and am using the same in case 2:
first I tried another definition of ch in case 2: assuming that if i=2
then char ch[] will not get allocated in case 1:. But the compiler gave
me (re-definition error) so i removed it and now its working fine.
Is it correct as per the standard?

switch (i)
{
case 1:
char ch[12];
strcpy(ch, "case 1\n");
cout<<ch;
break;
case 2:
strcpy(ch, "case 2\n"); //Can i use it here, when i=2, why
should the compiler allocate
// the variable shich is
declared in the case which is not true.
cout<<ch;
break;

default:
strcpy(ch, "default\n"); // Is it ok here also?
cout<<ch;
}
That I have done, But I wanted to know is the above behaviour as per
the standards and is guranteed to work always?

Jul 12 '06 #3
It is valid I think. You can just consider *case statement* as a
*label*.

Rahul wrote:
Forest wrote:
You can use { and } to meet your requirement.

[example]

switch ( i )
{
case 1:
{
char ch[1];
//...
break;
}
case 2:
{
char ch[2];
//...
break;
}
//...
}

[end example]

Rahul wrote:
Hi,
Is the following correct? Here i have declaread a char array in case 1:
and am using the same in case 2:
first I tried another definition of ch in case 2: assuming that if i=2
then char ch[] will not get allocated in case 1:. But the compiler gave
me (re-definition error) so i removed it and now its working fine.
Is it correct as per the standard?
>
switch (i)
{
case 1:
char ch[12];
strcpy(ch, "case 1\n");
cout<<ch;
break;
case 2:
strcpy(ch, "case 2\n"); //Can i use it here, when i=2, why
should the compiler allocate
// the variable shich is
declared in the case which is not true.
cout<<ch;
break;
>
default:
strcpy(ch, "default\n"); // Is it ok here also?
cout<<ch;
}

That I have done, But I wanted to know is the above behaviour as per
the standards and is guranteed to work always?
Jul 12 '06 #4
Rahul <ra*********@lucent.comwrote:
Hi,
Is the following correct? Here i have declaread a char array in case
1: and am using the same in case 2:
first I tried another definition of ch in case 2: assuming that if i=2
then char ch[] will not get allocated in case 1:. But the compiler
gave me (re-definition error) so i removed it and now its working
fine.
Is it correct as per the standard?
Afaik, no. The initialization of 'ch' might be skipped when 'i' is
2.
switch (i)
{
case 1:
char ch[12];
strcpy(ch, "case 1\n");
cout<<ch;
break;
case 2:
strcpy(ch, "case 2\n"); //Can i use it here, when i=2, why
should the compiler allocate
// the variable shich is
declared in the case which is not true.
cout<<ch;
break;

default:
strcpy(ch, "default\n"); // Is it ok here also?
cout<<ch;
}
hth
--
jb

(reply address in rot13, unscramble first)
Jul 12 '06 #5
Forest <Lu*******@gmail.comwrote:
You can use { and } to meet your requirement.
Please do not top-post. Always place it below the relevant part of
the message you are replying to. It just makes live easier for most of
us, as we are used to reading articles/books from top to bottom and not
from bottom to top.

regards
--
jb

(reply address in rot13, unscramble first)
Jul 12 '06 #6
Jakob Bieling wrote:
Rahul <ra*********@lucent.comwrote:
Hi,
Is the following correct? Here i have declaread a char array in case
1: and am using the same in case 2:
first I tried another definition of ch in case 2: assuming that if i=2
then char ch[] will not get allocated in case 1:. But the compiler
gave me (re-definition error) so i removed it and now its working
fine.
Is it correct as per the standard?

Afaik, no. The initialization of 'ch' might be skipped when 'i' is
2.
There is no initialization of 'ch' in the first place. So it's correct,
but similar code which /does/ initialize a variable in case 1 will
have a problem.

HTH,
Michiel Salters

Jul 12 '06 #7
Mi*************@tomtom.com wrote:
Jakob Bieling wrote:
>Rahul <ra*********@lucent.comwrote:
>>switch (i)
{
case 1:
char ch[12];
strcpy(ch, "case 1\n");
cout<<ch;
break;
> Afaik, no. The initialization of 'ch' might be skipped when 'i'
is 2.
There is no initialization of 'ch' in the first place. So it's
What about default initialization? The elements of the array need to
be initialized with something, even if it's an unspecified value, no?

regards
--
jb

(reply address in rot13, unscramble first)
Jul 12 '06 #8
Forest wrote:
It is valid I think. You can just consider *case statement* as a
*label*.

Please don't top-post. Your reply belongs following or interspersed
with properly trimmed quotes. See the newsgroup FAQ:
<http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.4or
<http://www.caliburn.nl/topposting.html>

Brian
Jul 12 '06 #9
Jakob Bieling wrote:
Mi*************@tomtom.com wrote:
There is no initialization of 'ch' in the first place. So it's

What about default initialization? The elements of the array need
to be initialized with something, even if it's an unspecified value,
no?
No, and in fact most automatic variables are not initialized. Using
such a variable is undefined behavior.

Brian
Jul 12 '06 #10
Default User <de***********@yahoo.comwrote:
Jakob Bieling wrote:
>Mi*************@tomtom.com wrote:
>>There is no initialization of 'ch' in the first place. So it's

What about default initialization? The elements of the array need
to be initialized with something, even if it's an unspecified value,
no?

No, and in fact most automatic variables are not initialized. Using
such a variable is undefined behavior.
True, that I knew. Thanks for pointing it out!
--
jb

(reply address in rot13, unscramble first)
Jul 12 '06 #11
Default User wrote:
Jakob Bieling wrote:
Mi*************@tomtom.com wrote:
There is no initialization of 'ch' in the first place. So it's
What about default initialization? The elements of the array need
to be initialized with something, even if it's an unspecified value,
no?

No, and in fact most automatic variables are not initialized. Using
such a variable is undefined behavior.
That statement isn't very good. You can do an number of things with
with the variable itself, such as assign to it or take its address. I
should have said using the VALUE of such a variable is undefined
behavior.

Brian

Jul 12 '06 #12

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

Similar topics

35
by: Thomas Matthews | last post by:
Hi, My son is writing a program to move a character. He is using the numbers on the keypad to indicate the direction of movement: 7 8 9 4 5 6 1 2 3 Each number has a direction except...
2
by: Cathleen C via DotNetMonster.com | last post by:
I'm a semi-beginner with c# and am having a problem effectively implementing a switch statement. I've created an asp.net app that runs a report depending on which item was selected from a drop...
4
by: Roy Gourgi | last post by:
Hi, My program has to be able to call on many classes that differ only by a number and rather than use a switch statement I was wandering if it is possible to do it in C# as it is possible in...
4
by: priyanka | last post by:
Hi there, I had a question. Is there any way of testing a string value in a switch statement. I have about 50 string values that can be in a string variable. I tried cheking them with the if...
3
by: Drowningwater | last post by:
I'm writing a program and I've made a switch statement. I have several quesions: 1. I have a string variable and when I try to use that string variable with the switch function I get a compiling...
6
by: asadikhan | last post by:
Hello, I have a bit of a design issue around this application I am developing, and I just want to run it through some of the brains out here. So I have a table called ErrorCheck which...
2
by: Miyagi | last post by:
I have a bunch of conditions in which to determine different types of questions to ask in a survey I am constructing. Just was wondering if anybody has used a two-dimensional switch statement. If...
8
by: Srinu | last post by:
Hi all, If we compile the below piece of code, it gets compiled. But gives weird result. switch(x) { int y=2; case 1:
5
by: mark4asp | last post by:
Every time the function below is called I get the alert. So I put a deliberate error in there and I check the value of (reportType=='MANDATE') in Firebug, which is found to be true. But still the...
2
by: Phillip B Oldham | last post by:
What would be the optimal/pythonic way to subject an object to a number of tests (based on the object's attributes) and redirect program flow? Say I had the following: pets = {'name':...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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...
0
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...

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.