为什么在嵌套 FOR 循环中使用相同的计数变量名称有效?
为什么下面没有报错?
for (int i=0; i<10; ++i) // outer loop
{
for (int i=0; i<10;++i) // inner loop
{
//...do something
}
//...do something else
}
按照我的理解,大括号 ({...}) 中的变量仅在这些大括号内的范围内。但内循环位于外循环的大括号内。因此,一旦我为内部循环声明 int i=0
,我是否应该得到有关多个定义的错误?
Why does the following not give an error?
for (int i=0; i<10; ++i) // outer loop
{
for (int i=0; i<10;++i) // inner loop
{
//...do something
}
//...do something else
}
The way I understand it, variables in braces ({...}) are in scope only within these braces. But the inner loop is inside the braces of the outer loop. So as soon as I declare int i=0
for the inner loop, shouldn't I get an error about multiple definitions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
理解这一点的最佳方法是,当您将 for 循环声明为位于该 for 循环的大括号内时,考虑 ( 和 ) 之间的所有内容,至少与作用域相关。
要理解这一点,请考虑一个没有声明 x 变量的函数,那么里面的以下代码将会给你一个错误。 (我们还假设您没有全局定义其他 x 变量。)
The best way to understand this is to think about everything between the ( and ) when you declare a for loop as being inside the braces of that for loop, at least as it relates to the scope.
To understand this, consider a function in which you have no x variable declared, then the following code inside will give you an error. (We are also assuming that you have no other x variable defined globally.)
内部循环启动另一个级别的作用域,for 循环在循环定义中启动作用域,因此第二个 i 位于新的作用域中。
请参阅
http://en.wikibooks.org/wiki/C%2B %2B_Programming/Scope#Scope_using_other_control_structs
The inner loop starts another level of scoping, for loops start the scope in the loop definition so the second i is in a new scope.
see
http://en.wikibooks.org/wiki/C%2B%2B_Programming/Scope#Scope_using_other_control_structures
您确定执行此操作时不会出现错误吗...?
您正在尝试在同一边界内定义两个 int 变量。因此将产生错误。在 C# 中,如果您尝试相同的代码,您将收到错误
Error 1 无法在此范围内声明名为“i”的局部变量,因为它会给“i”赋予不同的含义,而“i”已经是在“父级或当前”范围中使用来表示其他内容
ARE YOU SURE YOU DON'T GET AN ERROR WHILE EXECUTING THIS ....?
you are trying to define the two int variable within the same boundary .due to this this will generate the error . in c# if u try out the same code you will get the error
Error 1 A local variable named 'i' cannot be declared in this scope because it would give a different meaning to 'i', which is already used in a 'parent or current' scope to denote something else
您实际上正在创建一个与另一个变量同名的新变量。由于它们位于不同的作用域中,这是允许的,并且内部作用域中的变量“拥有”该名称。您将无法访问内部作用域内的外部作用域
i
。for 循环声明本身是 for 循环作用域的一部分,因此在第二个
i
的情况下算作内部作用域的一部分。You are actually making a new variable with the same name as another variable. Since they are in different scopes this is allowed, and the variable in the inner scope "owns" the name. You will not be able to access the outer-scoped
i
inside the inner scope.The for loop declaration itself is part of the scope of the for loop, so counts as part of the inner-scope in the case of the second
i
.C++ 编译器认为这是有效的,因为第二个的范围仅在 { } 大括号内。如果您在 C 中实现相同的功能,您将看到如下错误:
This is invalid in most C dialects;它是合法的 C++ 声明,因此如果您使用 C++ 编译器编译 C,则可能会被接受:
仅在 C++ 中的循环范围内有一个循环迭代器是很常见的,但 C 确保(特别是对于 C90,不是 C99),该声明超出了循环范围。希望这会有所帮助...:-)
因此,当您在旧循环中声明另一个 FOR 循环时,作用域会重新开始,并且您的代码在 C++ 或 C99 中编译时不会出现任何错误。这是范围声明通常接受的规范。
The C++ compiler accepts this as valid, as the scope of the second is only within the { } braces. If you implement the same in C, you will see an error like this:
This is illegal in most C dialects; it is a legal C++ declaration, and so may be accepted if you are compiling C with a C++ compiler:
It is common to have a loop iterator only in the scope of the loop in C++, but C makes sure (specially with the C90, not C99), that the declaration is outside the scope of the loop. Hope that helps ... :-)
So, when you declare another FOR loop within the older one, then the scope start fresh and your code compiles without any error in C++ or C99. This is the usual accepted norm for a scope declaration.