0

我编写了一个程序,该程序逐渐从一个包含一个月信息的简单结构发展为包含 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);
}
}
4

2 回答 2

2

直接的问题是由于这条线;

extern struct month months[12];

这里没有要引用的数组定义;months被声明在里面main,没有别的地方。摆脱那条线。

接下来你有逻辑问题:

days(months[12].monthnum);

您已经超出了数组的范围。数组是 0 索引的,即一个包含 12 个元素的数组包含有效的索引 0-11。12 太远了。

顺便说一句,当您显式初始化每个元素时,您不需要指定数组的维度。只需使用:

struct month months[] = {
    {"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},
};

编译器知道有十二个元素,因为你告诉它。现在,当/如果添加或删除元素时,您不必更改维度。

下一个问题;可变范围。你的days函数中有这个:

total += months[index].days;

好吧,因为是本地的,所以days无法访问。你需要学习和理解变量作用域。monthsmonthsmain

于 2013-03-26T05:45:46.060 回答
2

extern struct month months[12];months意味着您在某处定义了一个全局数组。您的months数组不是全局的,它是您的main函数的本地数组。这就是错误。其他消息不是第二个错误。

您还应该注意,您不应该访问months[12],因为该数组中只有 12 个元素 -months[0]months[11].

于 2013-03-26T05:49:26.573 回答