gpt4 book ai didi

c - IF 语句中定义的 undefined variable 出错

转载 作者:太空宇宙 更新时间:2023-11-04 00:50:31 24 4
gpt4 key购买 nike

尝试编译以下代码片段时:

#include <stdio.h>
#include <time.h>

void change_a(int * a) {
*a = 1;
return;
}

void main(void) {
int a = 0;
change_a(&a);
if (a) {
time_t start = time(NULL);
}
/* do something that never mentions a */

if (a) {
time_t end = time(NULL);
printf("Time spent = %ld\n", (int) end - start);
}
}

GCC 声明 start 变量在 printf 行中未定义:

$ gcc --version
gcc (Debian 4.8.2-1) 4.8.2
[SNIP]
$ gcc -o test.bin test.c
test.c: In function ‘main’:
test.c:24:44: error: ‘start’ undeclared (first use in this function)
printf("Time spent = %ld\n", (int) end - start);

另一方面,将 main 函数更改为:

void main(void) {
int a = 0;
time_t start = 0;

change_a(&a);
if (a) {
start = time(NULL);
}
...

问题一

是我做错了什么还是编译器做了一些我不知道的有趣事情?

我认为可能是编译器太聪明了,优化了那段代码,或者它的启发式方法失败了。但每次我“发现编译器错误”时,总是我遗漏了一些非常明显的错误。

因此,在我指责其他聪明人不够聪明之前,我宁愿让聪明人检查一下。特别是当问题在没有优化的情况下也发生时:

$ gcc -O0 -o test.bin test.c 
test.c: In function ‘main’:
test.c:24:44: error: ‘start’ undeclared (first use in this function)
printf("Time spent = %ld\n", (int) end - start);
^
test.c:24:44: note: each undeclared identifier is reported only once for each function it appears in

问题2

我还想知道是否有更好的方法来避免编译器错误(如果不是最后一个代码片段中的解决方法)。如果是我的代码错误(因为响应将包含“正确”代码),这是很明显的,但我也想知道如何在有人修复 GCC 中的(所谓的)错误时避免错误。

最佳答案

在您的第一个示例中,start 变量是在 if 语句的范围内声明的。因此,该变量在代码块末尾(右大括号 })超出范围。这绝对不是编译器错误。

您的“解决方法”是正确的解决方案。

See here有关变量作用域如何在 C/C++(以及许多其他使用 C 风格作用域的语言)中工作的更详尽的描述。

关于c - IF 语句中定义的 undefined variable 出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20531407/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com