我编写了一个程序,该程序逐渐从一个包含一个月信息的简单结构发展为包含 12 个非闰年相同信息结构的数组。现在,我正在尝试包含一个函数,“当给定月份编号时,返回一年中的总天数,包括该月份。假设问题 3 的结构模板和适当的此类结构数组已声明对外。”
当我按下运行时,我得到两个错误,我都不明白。它们是:“体系结构 x86_64 的未定义符号:“_months”,引用自:main.o ld 中的 _days:未找到体系结构 x86_64 的符号:错误:链接器命令失败,退出代码为 1(使用 -v 到见调用)”
任何帮助将不胜感激,谢谢。
#include <stdio.h>
int days(int monthnum);
struct month {
char name[10];
char abbreviaton[4];
int days;
int monthnum;
};
int main(void)
{
struct month months[12] = {
{"January", "Jan", 31, 1},
{"February", "Feb", 28, 2},
{"March", "Mar", 31, 3},
{"April", "Apr", 30, 4},
{"May", "May", 31, 5},
{"June", "Jun", 30, 6},
{"July", "Jul", 31, 7},
{"August", "Aug", 31, 8},
{"September", "Sep", 30, 9},
{"October", "Oct", 31, 10},
{"November", "Nov", 30, 11},
{"December", "Dec", 31, 12},
};
struct month *sign;
sign = &months[12];
days(months[12].monthnum);
return 0;
}
extern struct month months[12];
int days(int monthnum)
{
int index, total;
if (monthnum < 1 || monthnum > 12)
return(-1);
else
{
for (index = 0, total = 0; index < monthnum; index++)
total += months[index].days;
return(total);
}
}
这是我现在拥有的代码。该程序有效:
#include <stdio.h>
int days(int monthnum);
struct month {
char name[10];
char abbreviaton[4];
int days;
int monthnum;
};
struct month months[12] = {
{"January", "Jan", 31, 1},
{"February", "Feb", 28, 2},
{"March", "Mar", 31, 3},
{"April", "Apr", 30, 4},
{"May", "May", 31, 5},
{"June", "Jun", 30, 6},
{"July", "Jul", 31, 7},
{"August", "Aug", 31, 8},
{"September", "Sep", 30, 9},
{"October", "Oct", 31, 10},
{"November", "Nov", 30, 11},
{"December", "Dec", 31, 12},
};
int main(void)
{
int value;
int count = 0;
struct month *sign;
sign = &months;
printf("Enter month number: ");
scanf("%d", &months[count].monthnum);
value = days(sign->monthnum);
printf("%d", value);
return 0;
}
extern struct month months[];
int days(int monthnum)
{
int index, total;
if (monthnum < 1 || monthnum > 12)
return(-1);
else
{
for (index = 0, total = 0; index < monthnum; index++)
total += months[index].days;
return(total);
}
}