结构部分作为参数:未知类型名称

时间:2015-04-06 23:13:39

标签: c function structure

我试图将结构部分作为参数传递。但是,我在编译时收到以下消息:"未知类型名称' time1'"。这是我的代码导致这些问题的部分:

#include <stdio.h>

struct time {
    int hour;
    int minutes;
    int seconds;
};

struct time time1;
struct time time2;
struct time elapsed;

int hourDif (time1.hour, time2.hour) {
    if (time2.hour >= time1.hour) {
        elapsed.hour = time2.hour - time1.hour;
    }
    else {
        elapsed.hour = 24 - (time1.hour - time2.hour);
    }
    return 0;
}

1 个答案:

答案 0 :(得分:2)

int hourDif (time1.hour, time2.hour)

不是函数调用,它是函数声明,在您的情况下,它也是它的定义,您需要的是

int hourDif (struct time time1, struct time time2)
 {
    int difference;
    difference = 0; /* some compilers might complain */
    if (time2.hour >= time1.hour)
        difference = time2.hour - time1.hour;
    else
        difference = 24 - (time1.hour - time2.hour);
    return difference;
}

然后在代码中的某个地方,不需要全局变量

struct time time1;
struct time time2;

/* initialize `time1' and `time2' */
int difference  = hourDiff(time1, time2);