Defining a variable in switch statement

R

Rahul

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;
}
 
F

Forest

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]
 
R

Rahul

Forest said:
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]
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?
 
F

Forest

It is valid I think. You can just consider *case statement* as a
*label*.
Forest said:
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]
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?
 
J

Jakob Bieling

Rahul said:
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
 
J

Jakob Bieling

Forest said:
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
 
M

Michiel.Salters

Jakob said:
Rahul said:
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
 
J

Jakob Bieling

Jakob Bieling wrote:
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
 
D

Default User

Jakob said:
(e-mail address removed) wrote:

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
 
J

Jakob Bieling

Default User said:
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!
 
D

Default User

Default said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,134
Messages
2,570,780
Members
47,337
Latest member
HenriettaA

Latest Threads

Top