所以我有以下 C 代码:
for (int i = 0; i < nWallSegments; i+=4) {
fscanf(fin, "%le %le %le %le", &wallSegments[i+0], &wallSegments[i+1], &wallSegments[i+2], &wallSegments[i+3]);
}
for (int i = 0; i < nWallSegments; i+=4) {
nWallPoints += ceil(dist(wallSegments[i+0], wallSegments[i+1], wallSegments[i+2], wallSegments[i+3]) / dWallPoints) - 2;
// other stuff here
}
当我尝试编译时,出现以下错误。来自 Python 背景,我不知道发生了什么。我在网上搜索了答案,但一无所获。
Prog.c:44:12: error: redefinition of 'i'
for (int i = 0; i < nWallSegments; i+=4) {
^
Prog.c:40:12: note: previous definition is here
for (int i = 0; i < nWallSegments; i+=4) {c
最佳答案
这取决于编译器和/或标准 C 的版本。
在标准 C 1999 和更高版本(C99 和 C11)中,允许在循环本身中声明一个变量,此外,作用域只是循环,就像在 C++ 中一样。
通过使用一些现代 C 方言进行编译,您的代码必须能够正常运行。
我使用带有选项 -std=c99(或 c11)的 GCC。
在这种情况下,您的代码对我来说很顺利。
关于c - 重新定义 'i'错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26153179/