为什么这个联盟是删除第1记录在数组在c码?
题
这里是我的一个标题的文件,它由一个联盟的模板,有4个不同的结构。
#define MAX 3
union family
{
struct name /*for taking the name and gender of original member*/
{
unsigned char *namess;
unsigned int gender;
union family *ptr_ancestor; /*this is a pointer to his ancestors details*/
}names;
struct male /*for taking the above person's 3 male ancestors details if he is male*/
{
unsigned char husb_names[3][20];
unsigned char wife_names[3][20];
unsigned int wife_status[3];
}male_ancestor;
struct unmarry /*for taking the above person's 3 female parental ancestors if she is female and unmarried*/
{
unsigned int mar;
unsigned char parental_fem[3][20];
unsigned int marit_status[3];
}fem_un;
struct marry /*for taking 3 parental-in-laws details if she is female and married*/
{
unsigned int mar;
unsigned char in_law_fem[3][20];
unsigned int in_marit_status[3];
}fem_marr;
};
extern union family original[MAX]; /*for original person*/
extern union family ancestor_male[MAX]; /*used if he is male for storing his male ancestor details*/
extern union family ancestor_female[MAX]; /*used if she is female*/
extern int x;
我的目标是获得一个人的姓名和性别问题和存储个人的任何3名男性/女祖先,根据人的性别和婚姻状况如下..
我的意思是 MAX
会有3个成员及每个会具有3个祖先。这些祖先将由性别问题相应的部件,如下条件:
- 如果男性,然后使用
struct male
- 如果未婚女性使用
struct unmarry
- 如果女性结婚的使用
struct marry
struct name
是为成员的姓名和性别,对他们来说,我们必须采取的祖先和点的 *ptr_ancestor
至于相应的祖先array(ancestormale或ancestorfemale).
对象中存储器是一个联盟。"确定"。我的程序将有一系列的工会,在事实。每个元件阵列可以使用不同结构的联盟。在这里,我们应该谨慎分配的指针,否则我们可能失去我们的老年人记录在运行时间。
如果可能的话请告诉我如何获得的细节第1元。 original[0]
甚至后 original[1]
.我在这里就是最后一个元件阵列,以及所有以前的记录都没有了在运行时间。我没有使用任何其他数据结构或者文件。
我的环境是涡轮增压C。
解决方案
你需要读这个 问题是关于工会.你想要的东西更像是:
struct family {
struct name {
int gender;
int married;
blah
} names;
union {
struct male { blah } male_ancestor;
struct female_unmarried { blah } female_unmarried_ancestor;
struct female_married { blah } female_married_ancestor;
};
}
然后你可以试验的家庭。名字。性别和家庭。名字。结婚以确定哪些会员联盟使用。
其他提示
你可能误解的目的一个联盟。
一个联盟通常用来存储 一个 项目可以在几种形式。例如:
// Define an array of 20 employees, each identified either by name or ID.
union ID {
char name[10]; // ID may be a name up to 10 chars...
int serialNum; // ... or it may be a serial number.
} employees[20];
// Store some data.
employees[0].serialNum = 123;
strcpy(employees[1].name, "Manoj");
关键的区别 struct
和一个 union
那是一个 struct
是的集合体许多碎片的数据,但一个 union
是一个 overlayment:你可能只存储 一个 元素,因为它们都有同样的存储器。在上面的例子中,每个元素 employees[]
阵列包括10个字节,它是最小量的存储器可能举行 要么 10 char
s 或 1 int
.如果你参考 name
元素,你可以储存10 char
s.如果你参考 serialNum
元素,你可以储存1 int
(说的4个字节),并且不能访问的其余6个字节。
因此,我认为你要用不同的、独立的机构为代表的家庭成员。你做了什么似乎是填鸭式的几个方桩钉入一个圆圆的家庭作业。:-)
注意到高级的读者:请不要提及填充和词对准。他们很可能涵盖的下一个学期。:-)
你知道什么样的联盟意味着在C?你盟不会有3个成员。你的联盟有4个成员。其中4个成员,多少你想商店的价值观?
为什么你不问问你的TA?