468,458 Members | 1,820 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,458 developers. It's quick & easy.

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 1701
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

35 posts views Thread by Thomas Matthews | last post: by
2 posts views Thread by Cathleen C via DotNetMonster.com | last post: by
4 posts views Thread by Roy Gourgi | last post: by
4 posts views Thread by priyanka | last post: by
3 posts views Thread by Drowningwater | last post: by
2 posts views Thread by Miyagi | last post: by
5 posts views Thread by mark4asp | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.