C语言 使用 timeval 结构和 gettimeofday 的奇怪错误 — 因为 #define 中的分号



我遇到了一些奇怪的编译错误。 这是针对家庭作业的(帮助是可以的)。 这个想法是实现一个程序,测试用户每秒点击"输入"一次的能力。 我应该使用 gettimeofday 为每个"输入"获取一些时间值,然后找出平均时间和标准偏差...... 我正在尝试通过检查 stdin 中的""来做到这一点,然后如果为 true,则使用 gettimeofday 填充一个 timeval 结构,然后将所述结构存储在数组中以供以后使用......

我在编译时遇到的错误(gcc -Wextra homework1.c)是:

homework1.c: In function ‘main’:
homework1.c:19:29: error: expected ‘]’ before ‘;’ token
homework1.c:27:17: error: expected ‘)’ before ‘;’ token
homework1.c:32:4: error: ‘entry_array’ undeclared (first use in this function)
homework1.c:32:4: note: each undeclared identifier is reported only once for each function it appears in
我不明白为什么

我会收到前两个语法错误,然后我不明白为什么当我在"main"的开头明确声明"entry_array"时未声明它。 建议?

我觉得我因为不知道如何使用 timeval 结构而被烧伤...... 最初,我尝试像使用任何其他结构一样全局定义结构 timeval,但收到有关覆盖结构 timeval 定义的错误...... 这是因为它是在"sys/time.h"库中定义的吗?

代码如下:

  GNU nano 2.2.6                        File: homework1.c                                                       
//prototypes 
int GetAverage(long array[]);
int GetStdDev(long array[]);
//# of keystrokes tracked by user
#define MAX_STROKES 1;

int main(int argv, char ** argc) {
        struct timeval entry_array[MAX_STROKES]; //container for tv_usec fields from timeval struct
        double average = 0; 
        double std_deviation = 0;
        int count = 0; 
        printf("This program will test your ability to hit enter every 1 second, for 10 seconds.  Ready when yo$
        //loop to build array of timeval's
        while (count < MAX_STROKES) {
                struct timeval time_val;
                int input = getc(stdin);
                if (input == 'n') {
                        gettimeofday(&time_val, NULL);
                        entry_array[count] = time_val;
                        ++count;
                }
        }
        return 0;
}

问题是宏MAX_STROKES。由于这是家庭作业,我不会确切地告诉你它的问题是什么。

  1. 这: 无论您在哪里使用"MAX_STROKES",#define MAX_STROKES 1;都可能成为语法错误(您的工作是找出"为什么";))。

  2. 我希望你把这个注释掉了:GNU nano 2.2.6 File: homework1.c

  3. 我不确定你的"printf()"是否正常:在你的剪切/粘贴中,它在这里被切断了:Ready when yo$

  4. 我希望你 #includ 你需要的所有文件,比如"stdio.h"和"time.h"

做了

一些研究,并决定我MAX_STROKES宏观想法不太正确。 谢谢大家。 我的猜测是,它没有代表我想要的东西。 我正在寻找"int MAX_STROKES = 1"... 默认情况下 1 是字符吗? 我不太清楚它到底是什么。 阅读后,我决定使用"static const int MAX_STROKES = 1;"代替,它编译得很好。

相关内容

最新更新