goto指令跳过可变长度数组

时间:2014-04-07 19:59:08

标签: c

int main() {
    int sz = 10; 
    goto end;
    char bytes[sz];
end:
    return 0;
}

我在编译时遇到以下错误。我使用gcc和C99标准。

test.c: In function ‘main’:
test.c:3:2: error: jump into scope of identifier with variably modified type
test.c:5:1: note: label ‘end’ defined here
test.c:4:7: note: ‘bytes’ declared here

1 个答案:

答案 0 :(得分:14)

标准禁止:

C99 standard, paragraph 6.8.6.1

Constraints

[...] A goto statement shall not jump from outside the scope of an identifier having a 
      variably modified type to inside the scope of that identifier.

您的goto会跳过在运行时分配bytes数组的行。这是不允许的。

您可以通过花括号括起来限制bytes的范围,将分配放在goto和标签之前,或者根本不使用goto

更明确地说,一旦分配了bytes,你现在就在范围内。在分配之前,您在范围之外。所以你不能“从范围外跳”到“范围内”。

相关问题