C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Defining a variable in switch statement

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Rahul
Guest





PostPosted: Wed Jul 12, 2006 9:10 am    Post subject: Defining a variable in switch statement Reply with 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
Forest
Guest





PostPosted: Wed Jul 12, 2006 9:10 am    Post subject: Re: Defining a variable in switch statement Reply with 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:
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





PostPosted: Wed Jul 12, 2006 9:10 am    Post subject: Re: Defining a variable in switch statement Reply with quote



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





PostPosted: Wed Jul 12, 2006 9:10 am    Post subject: Re: Defining a variable in switch statement Reply with quote

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





PostPosted: Wed Jul 12, 2006 9:10 am    Post subject: Re: Defining a variable in switch statement Reply with quote

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





PostPosted: Wed Jul 12, 2006 9:10 am    Post subject: Re: Defining a variable in switch statement Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.