使用的#define分号的timeval结构和gettimeofday的 - 因为奇怪的错误 [英] Strange errors using timeval struct and gettimeofday —because of semicolon in #define

查看:262
本文介绍了使用的#define分号的timeval结构和gettimeofday的 - 因为奇怪的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个奇怪的夫妇编译错误的。这是一个家庭作业(帮助确定)。这样做是为了实现一个测试用户如何好打回车一旦第二个程序。我应该使用gettimeofday的得到一些时间值的每个输入,然后找出的平均时间是和标准差......我试图通过检查标准输入的'\\ n',然后,如果做到这一点如此,使用gettimeofday的填充以timeval结构,然后将其存储结构表示在以后使用数组...

在编译的时候,我发现了错误( GCC -Wextra homework1.c )是:

  homework1.c:在函数'主':
homework1.c:19:29:错误:之前的预期']'';'令牌
homework1.c:27:17:错误:之前的预期')'';'令牌
homework1.c:32:4:错误:entry_array'未声明(在此函数第一次使用)
homework1.c:32:4:注:每个未声明的标识符为它出现在每个函数报道只有一次

我不明白为什么我得到的前两个语法错误,然后我不明白为什么entry_array是未申报的时候,我清楚的开始宣布它的主。建议?

我觉得我越来越被不知道如何使用的timeval结构烧毁......起初我试图定义timeval结构全局就像你使用任何其他结构,但渐渐会覆盖的结构定义的错误timeval中......这是因为它是在SYS / time.h中库中定义?

这里的code:

  GNU纳米2.2.6文件:homework1.c
//原型
INT GetAverage(多头排列[]);
INT GetStdDev(多头排列[]);//击键#用户跟踪
#定义MAX_STROKES 1;INT主(INT的argv,焦炭**的argc){
        timeval结构entry_array [MAX_STROKES] //从timeval中结构tv_usec领域容器
        双平均= 0;
        双std_deviation = 0;
        诠释计数= 0;        的printf(该计划将考验你的命中进入每隔1秒,10秒的能力。当准备哟$        //循环建立timeval中的数组
        而(COUNT< MAX_STROKES){
                timeval结构time_val;
                INT输入= GETC(标准输入);
                如果(输入=='\\ n'){
                        函数gettimeofday(安培; time_val,NULL);
                        entry_array [计数] = time_val;
                        ++计数;
                }
        }
        返回0;
}


解决方案

的问题是 MAX_STROKES 宏。由于这是功课,我不会告诉你它存在的问题到底是什么。

I am getting a couple of weird compilation errors. This is for a homework assignment (help is ok). The idea is to implement a program that tests how well the user can hit "enter" once a second. I'm supposed to use gettimeofday to get some time values for each "enter" and then find out what the average time is and standard deviation... I'm trying to do this by checking stdin for '\n' and then if true, using gettimeofday to populate a timeval struct, then store said struct in an array for later use...

The errors I'm getting when compiling (gcc -Wextra homework1.c) are:

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

I can't see why I'm getting those first two syntax errors and then I can't understand why "entry_array" is undeclared when I am clearly declaring it in the beginning of "main." Suggestions?

I feel like I'm getting burned by not knowing how to use the timeval struct... Initially I tried to define the struct timeval globally like you would with any other struct but was getting an error about overwriting the definition for struct timeval... Is this because it is defined in the "sys/time.h" library?

Here's the code:

  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;
}

解决方案

The problem is the MAX_STROKES macro. As this is homework, I won't tell you exactly what the problem with it is.

这篇关于使用的#define分号的timeval结构和gettimeofday的 - 因为奇怪的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆