标号语句 与 变量定义

标号语句有:goto、case ,用 vc 6.0  goto 和case里头定义变量都提示错误,vc 在函数执行语句开始后就不能再定义或声明变量了。(dev c++未测试)

以下是官方解释:见 http://en.cppreference.com/w/c/language

Explanation
The goto statement causes an unconditional jump (transfer of control) to the statement prefixed by the named label (which must appear in the same function as the goto statement), except when this jump would enter the scope of a variable-length array or another variably-modified type. (since C99)
A label is an identifier followed by a colon (:) and a statement. Labels are the only identifiers that have function scope: they can be used (in a goto statement) anywhere in the same function in which they appear. There may be multiple labels before any statement.
Entering the scope of a non-variably modified variable is permitted:
goto lab1; // OK: going into the scope of a regular variable
    int n = 5;
lab1:; // Note, n is uninitialized, as if declared by int n; n值未知
 
//   goto lab2;   // Error: going into the scope of two VM types
     double a[n]; // a VLA
     int (*p)[n]; // a VM pointer
lab2:

OK: going into the scope of a regular variable 和 Note, n is uninitialized, as if declared by int n; 这两句话的意思应该是goto对定义的普通变量,但只是跳过了初始化,没有跳过声明,所以声明还是会执行;

猜你喜欢

转载自blog.csdn.net/xmzzy2012/article/details/79348354
今日推荐