在switch语句中定义变量 [英] Defining a variable in switch statement
问题描述
以下是否正确?在这里我已经在例1中声明了一个char数组:
并且在情况2中使用相同的结果:
首先我尝试了案例2中ch的另一个定义:假设如果i = 2
然后char ch []将不会在案例1中分配:但编译器给了我
me(重新定义错误),所以我删除了它,现在工作正常。
按照标准是否正确?
开关(i)
{
案例1:
char ch [12];
strcpy(ch," case 1 \ n");
cout<< ch;
break;
case 2:
strcpy(ch,case 2 \ n); //我可以在这里使用它,当i = 2时,为什么
应该编译器分配
//变量shich是
声明的这个案子不是真的。
cout<< ch;
休息;
默认:
strcpy(ch," default\\\
"); //这也可以吗?
cout<< ch;
}
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;
}
推荐答案
你可以使用{和}来满足你的要求。
[例子]
开关(i)
{
案例1:
{
char ch [1];
/ / ...
休息;
}
案例2:
{
char ch [2];
// ...
休息;
}
//。 ..
}
[结束示例]
Rahul写道:
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:
以下是否正确?在这里我已经在例1中声明了一个char数组:
并且在情况2中使用相同的结果:
首先我尝试了案例2中ch的另一个定义:假设如果i = 2
然后char ch []将不会在案例1中分配:但编译器给了我
me(重新定义错误),所以我删除了它,现在工作正常。
按照标准是否正确?
开关(i)
{
案例1:
char ch [12];
strcpy(ch," case 1 \ n");
cout<< ch;
break;
case 2:
strcpy(ch,case 2 \ n); //我可以在这里使用它,当i = 2时,为什么
应该编译器分配
//变量shich是
声明的这个案子不是真的。
cout<< ch;
休息;
默认:
strcpy(ch," default\\\
"); //这里也可以吗?
cout<< ch;
}
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;
}
Forest写道:
Forest wrote:
您可以使用{和}来满足您的要求。
[例子]
开关(i)
{
案例1:
{
char ch [1];
// ...
休息;
}
案例2:
{
char ch [2];
// ...
休息;
}
// ...
}
[结束示例]
Rahul写道:
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:
以下是否正确?在这里我已经在例1中声明了一个char数组:
并且在情况2中使用相同的结果:
首先我尝试了案例2中ch的另一个定义:假设如果i = 2
然后char ch []将不会在案例1中分配:但编译器给了我
me(重新定义错误),所以我删除了它,现在工作正常。
按照标准是否正确?
开关(i)
{
案例1:
char ch [12];
strcpy(ch," case 1 \ n");
cout<< ch;
break;
case 2:
strcpy(ch,case 2 \ n); //我可以在这里使用它,当i = 2时,为什么
应该编译器分配
//变量shich是
声明的这个案子不是真的。
cout<< ch;
休息;
默认:
strcpy(ch," default\\\
"); //这里也可以吗?
cout<< ch;
}
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?
我认为这是有效的。您可以将* case语句*视为
*标签*。
Rahul写道:
It is valid I think. You can just consider *case statement* as a
*label*.
Rahul wrote:
Forest写道:
Forest wrote:
您可以使用{和}来满足您的要求。
[示例]
开关(i)
{
案例1:
{
char ch [1];
// ...
休息;
}
案例2:
{
char ch [2];
// ...
休息;
}
// ...
}
[最后的例子]
>
Rahul写道:
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:
以下是否正确?在这里我已经在例1中声明了一个char数组:
并且在情况2中使用相同的结果:
首先我尝试了案例2中ch的另一个定义:假设如果i = 2
然后char ch []将不会在案例1中分配:但编译器给了我
me(重新定义错误)所以我删除了它,现在工作正常。
按照标准是否正确?
>
开关(i)
{
案例1:
char ch [12] ;
strcpy(ch," case 1 \ n");
cout<< ch;
break;
案例2:
strcpy(ch," case 2 \ n"); //我可以在这里使用它,当i = 2时,为什么
应该编译器分配
//变量shich是
声明的这个案子不是真的。
cout<< ch;
休息;
>
默认:
strcpy(ch," default\\\
"); //这也可以吗?
cout<< ch;
}
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?
这篇关于在switch语句中定义变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!