switch语句“error C2630: initialization of 'k' is skipped by 'case' label”编译错误
2015-11-22 11:26
661 查看
MSDN说明如下:
compiler error c2360 : initialization of identifier is skipped by case label
the specified identifier initialization can be skipped in a switch statement.
it is illegal to jump past a declaration with an initializer unless the declaration is enclosed in a block.
the scope of the initialized variable lasts until the end of the switch statement unless it is declared in an enclosed block within the switch statement.
the following is an example of this error:
在switch语句内定义一个变量的时候,如果不在一个语句块内,它是直到遇到switch的"}"才结束的。
int i = 1;错误,错就错在它是以switch的"}"结束的,此时被case 1:语句跳过,int i = 1不会被初始化。
int j = 1;它是遇到下面的"}"就结束了,因此正确。
int k = 1;它虽然没有在一个语句块中,但它的下一个结束"}"正好就是switch的"}",不会被跳过,因此也正确。
解决方案:1.如果有在case内定义新变量,最好将该条case内的语句加上{}构成语句块,避免出错!
2.在case外部定义变量。
compiler error c2360 : initialization of identifier is skipped by case label
the specified identifier initialization can be skipped in a switch statement.
it is illegal to jump past a declaration with an initializer unless the declaration is enclosed in a block.
the scope of the initialized variable lasts until the end of the switch statement unless it is declared in an enclosed block within the switch statement.
the following is an example of this error:
} |
int i = 1;错误,错就错在它是以switch的"}"结束的,此时被case 1:语句跳过,int i = 1不会被初始化。
int j = 1;它是遇到下面的"}"就结束了,因此正确。
int k = 1;它虽然没有在一个语句块中,但它的下一个结束"}"正好就是switch的"}",不会被跳过,因此也正确。
解决方案:1.如果有在case内定义新变量,最好将该条case内的语句加上{}构成语句块,避免出错!
2.在case外部定义变量。
相关文章推荐
- PHP8新特性之match表达式
- 如何组织构建多文件 C 语言程序(二)
- 访问Nginx发生SSL connection error的一种情况
- 如何写好 C main 函数
- 星外虚拟主机访问被控出现Unspecified error解决方法
- IIS 错误 Server Application Error 详细解决方法
- On Error Resume Next 语句
- IIS运行错误 Server Application Error 错误代码 Error: 8004的解决方法
- 三个有用的 Switch 命令
- Lua和C语言的交互详解
- Lua编程示例(一):select、debug、可变参数、table操作、error
- 收集整理的http/1.1 500 server error错误的解决方法
- SQL 2005 ERROR:3145 解决办法(备份集中的数据库备份与现有的数据库不同)
- VBScript中On Error语句用法小结
- 关于C语言中参数的传值问题
- 简要对比C语言中三个用于退出进程的函数
- 深入C++中API的问题详解
- 基于C语言string函数的详解
- C语言中fchdir()函数和rewinddir()函数的使用详解
- C语言内存对齐实例详解