开发者

declare variable in switch? [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicate:

Why can't variables be declared in a switch statement?

switch (i){
 case 'i': int i; break; } //it works


switch (i){
 case 'i': int i;i=0; break; } //it also works


switch (i){
 case 'i': 开发者_如何学Pythonint i=0; break; } //it ain't


Dunno the error, but this also works for me in Objective-C (strict superset of C, although I don't believe that 1) and I do this always, even when I simply return something:

switch (i) {

  case 'i': { // <- curly brackets
    int i = 0; break;
  } // <- curly brackets

}

(1 Because a variable can be named id in C but not in Objective-C)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜