我试图在我的程序中使用结构,并且我收到一堆错误消息,指出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;
}
答案 0 :(得分:1)
您无需定义三个结构,因为它们的类型相同 - 时间。您只能定义一个结构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;
}
答案 1 :(得分:0)
在此功能定义中
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);
}
编译器将标识符time1和time2视为对象的名称。但它们没有定义,因为你没有指定它们的类型。我认为您忘记指定对象的名称并仅指定其类型。函数声明可能看起来像
int timeDif(struct time1 t1, struct time2 t2);
考虑到必须在结构类型的对象声明中使用结构名称的结构标记。
此函数虽然返回类型为int,但不返回任何内容。它没有任何意义。并且您可能无法在C中定义其结构中的结构的数据成员。
因此这个结构定义
struct dif {
int secDif = time2.seconds2 - time1.seconds1;
int minDif = time2.minutes2 - time1.minutes1;
int hourDif = time2.hours2 - time1.hours1;
};
无效。
函数调用
存在同样的问题timeDif(time1, time2);
我认为你的意思是以下
#include <stdio.h>
struct time
{
int seconds;
int minutes;
int hours;
};
struct time_dif
{
int secDif;
int minDif;
int hourDif;
};
struct time_dif timeDif( struct time t1, struct time t2 )
{
struct time_dif dif =
{
t2.seconds - t1.seconds,
t2.minutes - t1.minutes,
t2.hours - t1.hours
};
return dif;
}
int main(void)
{
struct time t1, t2;
printf( "Enter hours1 (hh): " );
scanf( "%d", &t1.hours );
printf( "Enter minutes1 (mm): " );
scanf( "%d", &t1.minutes );
printf( "Enter seconds1 (ss): " );
scanf( "%d", &t1.seconds );
printf( "Enter hours2 (hh): " );
scanf( "%d", &t2.hours );
printf( "Enter minutes2 (mm): " );
scanf( "%d", &t2.minutes );
printf( "Enter seconds2 (ss): " );
scanf( "%d", &t2.seconds );
struct time_dif dif = timeDif( t1, t2 );
printf( "The difference is %d:%d:%d\n", dif.hourDif, dif.minDif, dif.secDif );
return 0;
}
考虑到例如分钟的差异可以是负值。你应该重写函数timeDid
,以便它能提供两次正确的差异。
答案 2 :(得分:0)
您的代码中存在大量错误。我在你的代码中做了一些修改。
#include <stdio.h>
struct time1 {
int seconds1;
int minutes1;
int hours1;
};
struct time2 {
int seconds2;
int minutes2;
int hours2;
};
int timeDif(struct time1 time1, struct time2 time2) {
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) {
//declare the variable to use.
struct time1 time1;
struct time2 time2;
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;
}
答案 3 :(得分:0)
每当您提及任何struct
或class
时,您都必须创建objects
。您无法直接使用它们,例如scanf("%d", &time1.seconds1);
。相反,首先创建一个time1
对象,然后使用该对象。这很简单:
struct time1 t1; // creates an object t1 of the type time1.
scanf("%d",&t1.seconds1);
主要原因是:当您定义struct
时,实际上是在定义type
,就像int
或char
数据类型一样。你永远不会像scanf("%d",&int);
那样直接使用它们。您始终创建int
类型的变量,然后进一步使用它。同样,您为struct创建objects
,然后进一步使用它们。
此外,函数timeDif
几乎没有问题,例如它没有返回任何值。因为看起来你的函数在它已经打开结果后需要返回任何内容,所以将它声明为void
。此外,您没有理由在此函数中包含struct
,因为您在同一个函数中打印结果,并且没有在任何地方使用该结构,并且此结构也容易出错。 。
以下是您的代码的修改版本:http://ideone.com/uzBnVS