gpt4 book ai didi

c - 结构: Undeclared identifiers

转载 作者:行者123 更新时间:2023-11-30 15:21:54 28 4
gpt4 key购买 nike

我尝试在程序中使用结构,但收到一堆错误消息,指出 time1、time2 和 hourDif 是未声明的标识符。我以为我按照你应该的方式声明了这些结构,所以我对问题是什么感到困惑。

代码如下:

#include <stdio.h>

struct time1 {
int seconds1;
int minutes1;
int hours1;
};

struct time2 {
int seconds2;
int minutes2;
int hours2;
};

int timeDif(time1, time2) {
struct dif {
int secDif = time2.seconds2 - time1.seconds1;
int minDif = time2.minutes2 - time1.minutes1;
int hourDif = time2.hours2 - time1.hours1;
};

printf("The difference is %d:%d:%d\n", hourDif, minDif, secDif);
}



int main(void) {

printf("Enter seconds1 (ss)\n");
scanf("%d", &time1.seconds1);

printf("Enter minutes1 (mm)\n");
scanf("%d", &time1.minutes1);

printf("Enter hours1 (hh)\n");
scanf("%d", &time1.hours1);

printf("Enter seconds2 (ss)\n");
scanf("%d", &time2.seconds2);

printf("Enter minutes2 (mm)\n");
scanf("%d", &time2.minutes2);

printf("Enter hours2 (hh)\n");
scanf("%d", &time2.hours2);

timeDif(time1, time2);

return 0;
}

最佳答案

您不需要定义三个结构体,因为它们是相同的类型——时间。您只能定义一个结构体time:

#include <stdio.h>

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

void timeDif(struct time time1,struct time time2) {
struct time diff= {
time2.seconds - time1.seconds,
time2.minutes - time1.minutes,
time2.hours - time1.hours
};

printf("The difference is %d:%d:%d\n", diff.hours, diff.minutes, diff.seconds);
}

int main(void) {
struct time time1,time2,diff;
printf("Enter seconds1 (ss)\n");
scanf("%d", &time1.seconds);

printf("Enter minutes1 (mm)\n");
scanf("%d", &time1.minutes);

printf("Enter hours1 (hh)\n");
scanf("%d", &time1.hours);

printf("Enter seconds2 (ss)\n");
scanf("%d", &time2.seconds);

printf("Enter minutes2 (mm)\n");
scanf("%d", &time2.minutes);

printf("Enter hours2 (hh)\n");
scanf("%d", &time2.hours);

timeDif(time1, time2);

return 0;
}

关于c - 结构: Undeclared identifiers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29442881/

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