我是新手,我需要调试代码的帮助。 当我编译它时,“形式参数 1 的类型不完整”和形式参数 2 的类型不完整”错误出现在
printf("Age is %d years.\n", calc_age(birth, current));
而“参数 1 ('birth') 的类型不完整”和“参数 2 ('current') 的类型不完整”错误出现在
int calc_age (struct date birth, struct date current) {
感谢帮助,谢谢!
#include <stdio.h>
int calc_age (struct date birth, struct date current);
int main(void)
{
struct date {
int month[1];
int day[1];
int year[1];
};
struct date birth, current;
char c;
printf("Input the birthdate (MM/DD/YY): ");
scanf("%d %c %d %c %d", birth.month, &c, birth.day, &c, birth.year);
printf("Input date to calculate (MM/DD/YY): ");
scanf("%d %c %d %c %d", current.month, &c, current.day, &c, current.year);
printf("Age is %d years.\n", calc_age(birth, current));
return 0;
}
int calc_age (struct date birth, struct date current) {
int age;
if (birth.month[0] < current.month[0]) {
age = (current.year[0] - birth.year[0]);
} else if (birth.month[0] > current.month[0]) {
age = (current.year[0] - birth.year[0] - 1);
} else {
if (birth.day[0] <= current.day[0]) {
age = (current.year[0] - birth.year[0]);
} else {
age = (current.year[0] - birth.year[0] - 1);
}
}
return age;
}
最佳答案
您的程序显示不完整类型
错误,因为struct date
的范围仅限于main()
函数。在 main()
之外,结构定义不可见。
因此,struct date
定义应该在全局范围内,以便从 calc_age()
(也许还有其他函数)可见。如果您可以为此目的创建并维护一个头文件,那就更好了。
也就是说,在您的代码中,根据您当前的要求,删除结构中的单元素数组,例如
struct date {
int month;
int day;
int year;
};
还有 scanf()
语句
scanf("%d %c %d %c %d", birth.month, &c, birth.day, &c, birth.year);
应该阅读
scanf("%d %c %d %c %d", &birth.month, &c, &birth.day, &c, &birth.year);
关于c - 如何解决 "parameter has incomplete type"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29371704/