gpt4 book ai didi

c - 结构部分作为参数 : unknown type name

转载 作者:太空宇宙 更新时间:2023-11-04 08:28:11 25 4
gpt4 key购买 nike

我正在尝试将结构部分作为参数传递。但是,我在编译时收到以下消息:“未知类型名称‘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);

关于c - 结构部分作为参数 : unknown type name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29481188/

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