 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Rahul Guest
|
Posted: Wed Jul 12, 2006 9:10 am Post subject: 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;
} |
|
| Back to top |
|
 |
Forest Guest
|
Posted: Wed Jul 12, 2006 9:10 am Post subject: Re: Defining a variable in switch statement |
|
|
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:
| Quote: | 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;
} |
|
|
| Back to top |
|
 |
Jakob Bieling Guest
|
Posted: Wed Jul 12, 2006 9:10 am Post subject: Re: Defining a variable in switch statement |
|
|
Forest <LuckyYong (AT) gmail (DOT) com> wrote:
| Quote: | 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) |
|
| Back to top |
|
 |
Rahul Guest
|
Posted: Wed Jul 12, 2006 9:10 am Post subject: Re: Defining a variable in switch statement |
|
|
Forest wrote:
| Quote: | 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? |
|
| Back to top |
|
 |
Jakob Bieling Guest
|
Posted: Wed Jul 12, 2006 9:10 am Post subject: Re: Defining a variable in switch statement |
|
|
Rahul <rahulsharma (AT) lucent (DOT) com> wrote:
| Quote: | 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.
| Quote: | 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) |
|
| Back to top |
|
 |
Forest Guest
|
Posted: Wed Jul 12, 2006 9:10 am Post subject: Re: Defining a variable in switch statement |
|
|
It is valid I think. You can just consider *case statement* as a
*label*.
Rahul wrote:
| Quote: | 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? |
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|