By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,957 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

Defining a variable in switch statement

P: n/a
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
Share this Question
Share on Google+
11 Replies


P: n/a
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

P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.