对于为什么会出现此错误,我确实感到困惑。我的代码可以正常编译,但我希望收到0条警告。在搜索了一下stackoverflow之后,没有人拥有与我正在使用的相同的结构类型。下面是我的代码片段(这些是由名字组成的)
如果它有助于将我的MAXCONTACTS设置为5
struct Contact conNum[MAXCONTACTS] = { { {"Rick", { '\0' }, "Grimes" },
{11, "Trailer Park", 0, "A7A 2J2", "King City" },
{"4161112222", "4162223333", "4163334444" } },
{
{"Maggie", "R.", "Greene" },
{55, "Hightop House", 0, "A9A 3K3", "Bolton" },
{"9051112222", "9052223333", "9053334444" } },
{
{"Morgan", "A.", "Jones" },
{77, "Cottage Lane", 0, "C7C 9Q9", "Peterborough"},
{"7051112222", "7052223333", "7053334444" } },
{
{"Sasha", {'\0'}, "Williams" },
{55, "Hightop House", 0, "A9A 3K3", "Bolton"},
{"9052223333", "9052223333", "9054445555" } },
};
编辑:这是我的结构声明
// Structure type Name declaration
struct Name {
char firstName[31];
char middleInitial[7];
char lastName[36];
};
struct Address {
int streetNumber;
int apartmentNumber;
char street[41];
char postalCode[8];
char city[41];
};
struct Numbers {
char cell[11];
char home[11];
char business[11];
};
struct Contact {
struct Name name;
struct Address address;
struct ``Numbers numbers;
};
答案 0 :(得分:1)
{11, "Trailer Park", 0, "A7A 2J2", "King City" },
此初始化用于
struct Address {
int streetNumber;
int apartmentNumber;
char street[41];
char postalCode[8];
char city[41];
};
如您所见,您正在尝试将"Trailer Park"
初始化为int apartmentNumber;
。
这就是为什么您会收到警告。
因此将结构的定义更改为
struct Address {
int streetNumber;
char street[41];
int apartmentNumber;
char postalCode[8];
char city[41];
};
仅在一个地方
或
如下所示在所有位置更改初始化顺序。
{11, 0, "Trailer Park", "A7A 2J2", "King City" },
答案 1 :(得分:1)
您的struct Address
包含两个int
字段,后跟两个char
数组:
struct Address {
int streetNumber;
int apartmentNumber;
char street[41];
char postalCode[8];
char city[41];
};
但是您不能按此顺序初始化它们:
{11, "Trailer Park", 0, "A7A 2J2", "King City" },
除非使用命名的初始化程序,否则必须按顺序指定字段。首先输入街道编号,然后输入公寓编号,然后 街道:
{11, 0, "Trailer Park", "A7A 2J2", "King City" },
与其他三个相同。