读书人

switch语句中case跳过变量初始化的有关

发布时间: 2012-05-27 05:42:30 作者: rapoo

switch语句中case跳过变量初始化的问题

C/C++ code
typedef struct tagPOINTS{    short   y;    short   x;} POINTS;int a = 0;switch (a){    case 0:        POINTS p = *(POINTS *)a;        int i = 0;        break;    case 1:        break;}


编译报错:
error C2360: initialization of 'i' is skipped by 'case' label

这我能理解。
问题是,为什么没有对POINTS报错?

[解决办法]
我的2005报了...
error C2360: initialization of 'i' is skipped by 'case' label
error C2360: initialization of 'p' is skipped by 'case' label
[解决办法]
6.0有问题吧
[解决办法]
应该和编译器有关吧,有的两个都报,有的报一个,用gcc编好像不报啊
[解决办法]
借用4楼地回答
MSDN解释:Compiler Error C2360 initialization of 'identifier' is skipped by 'case' label
The initialization of identifier can be skipped in a switch statement. You cannot jump past a declaration with an initializer unless the declaration is enclosed in a block. (Unless it is declared within a block, the variable is within scope until the end of the switch statement.)
变量的初始化可能被switch语句。除非把变量定义放在一个程序块里面,否则不能跳过带有初始化的变量定义。(除非定义在一个程序块里,变量的作用域直到switch语句结束)

至于VC6不报错,VC2005报错,那就跟编译器有关。
MSDN是说会被跳过,所以有的编译器作了严格限制,有的没有!

[解决办法]
以前讨论过~
[解决办法]
哦,我也没看清题目
POINTS p = *(POINTS *)a;
不报错误问题应该和编译器有关,本人没有研究过编译原理
[解决办法]
应该是6.0编译器的失误!

这样的牛帖不顶!岂不浪费!
up

读书人网 >C++

热点推荐