结构部分作为参数:未知类型名称
Structures portions as argument: unknown type name
我正在尝试将结构部分作为参数传递。但是,我在编译时收到以下消息:"unknown type name '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;
}
这个
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);
我正在尝试将结构部分作为参数传递。但是,我在编译时收到以下消息:"unknown type name '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;
}
这个
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);