我有一个嵌套结构的问题。程序初始化结构数组,其中结构的一个成员是另一个结构。如果第二个名字给该程序检查,并提出在形式上完全数据嵌套结构的成员以某种方式附加到结构的前一个成员
`Last_name, First name, (First character of the second name if it had been given). -- number
的问题是,我不知道为什么,但不知何故,名字被添加到PESEL构件的端部和输出我们越来越
Jordan, Michael J. -- 65092301159Michael
T, Thomas -- 985Thomas
Christie, Agatha S. -- 25941325923Agatha
代替
Jordan, Michael J. -- 65092301159
T, Thomas -- 985
Christie, Agatha S. -- 25941325923
下面是一个代码,它采用C99为指定的初始化功能:
#include <stdio.h>
#include <string.h>
#define SIZE1 11
#define SIZE2 81
#define N 3
struct person
{
char name[SIZE2];
char second_name[SIZE2];
char last_name[SIZE2];
};
struct one
{
char PESEL[SIZE1];
struct person data;
};
void show(struct one *, int);
int main(void)
{
struct one personal_data[N] =
{
{
"65092301159",
{
"Michael",
"James",
"Jordan"
}
},
{
"985",
{
.name = "Thomas",
.last_name = "T"
}
},
{
"25941325923",
{
"Agatha",
"Sam",
"Christie"
}
}
};
int i;
for(i = 0; i < N; i++)
{
if(strlen(personal_data[i].data.second_name) != 0)
show(&personal_data[i], 1);
else
show(&personal_data[i], 0);
}
return 0;
}
void show(struct one *pt, int flag)
{
if(flag)
printf("%s, %s %c. -- %s\n",pt->data.last_name, pt->data.name, pt->data.second_name[0] , pt->PESEL);
else
printf("%s, %s -- %s\n",pt->data.last_name, pt->data.name , pt->PESEL);
}
sizeof(“65092301159”)是12. – BLUEPIXY