goto:[Error] jump into scope of identifier with variably modified type

C语言 GOTO 你不知道的事情

错误Code

#include <stdio.h>
int main()
{
    
        
    int a=1,b=2;    
    goto End;	
    int c[b];
End:	
    return 0;
} 

编译器报错内容

In function 'main':[Error] jump into scope of identifier with variably modified type
[Note] label 'End' defined here
[Note] 'c' declared here

为什么会出现

根据C11标准:
A goto statement is not allowed to jump past any declarations of objects with variably modified types. A jump within the scope, however, is permitted.

也就是说goto语句与“标签”之间的code不允许有可变长数组的声明语句。因为goto是无条件跳转语句,需要在编译时确定地址,如果可变长数组夹在其中,则编译器无法确定地址。
以下code都正确

#include <stdio.h>
int main()
{
    
        
    goto End;	
    int a=1,b=2;	
    int c[2];
End:	
    return 0;	
} 

怎么解决

将可变长数组的定义移动到“goto-tag”范围之外

#include <stdio.h>
int main()
{
    
    	
    int a=1,b=2;	
    int c[a];	
Begin:     
    goto End;
End:	
    return 0;	
} 

喜欢就一键三连吧!!!

猜你喜欢

转载自blog.csdn.net/qq_31985307/article/details/114128410