微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

c – 为什么没有初始化器跳进标量类型对象的范围?

当我正在阅读C标准时,根据标准,以下代码似乎完全没问题.
int main() {
   goto lol;
   {
      int x;
lol:
      cout << x << endl;
   }
}

// OK

[n3290: 6.7/3]: It is possible to transfer into a block,but not in a
way that bypasses declarations with initialization. A program that
jumps from a point where a variable with automatic storage duration is
not in scope to a point where it is in scope is ill-formed unless the
variable has scalar type
,class type with a trivial default
constructor and a trivial destructor,a cv-qualified version of one of
these types,or an array of one of the preceding types and is declared
without an initializer
.

它为什么要起作用?跳过它的定义并使用undefined x不是很危险吗?为什么初始化器的存在会有什么不同?

解决方法

无论如何你都会使用未初始化的x,因为int x;就像它会得到的那样没有初始化.初始化程序的存在当然是有区别的,因为你要跳过它. int x = 5;例如,初始化x,所以如果你跳过它就会有所不同.

原文地址:https://www.jb51.cc/c/110507.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐