为什么这个工会删除第一个记录数组在c code? [英] Why this union is deleting the 1st records in arrays in the c code?
问题描述
下面是我的头文件之一,它由4种不同结构的工会模板。
Here is one of my header file which consists of a union template with 4 different structures.
#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个男/女祖先根据人的性别,婚姻状况,如下所示。
My aim is to get a person name and gender and store the person's any 3 male/female ancestors according to the person's gender and marital status as follows..
我的意思是 MAX
将有3个成员,每家都会有3个祖先。这些祖先会由性别来决定,如下列条件相应的成员:
I mean that MAX
will have 3 members and each will be having 3 ancestors. These ancestors will be determined by the gender the corresponding member like the following conditions:
- 如果男性然后用
结构男性
- 如果未婚女性使用
结构和...离婚
- 如果已婚女性使用
结构结婚
- if male then use
struct male
- if female unmarried use
struct unmarry
- if female married use
struct marry
结构名称
为成员的名字和性别的人,我们必须采取祖先和点 * ptr_ancestor
到相应的祖先阵列(ancestormale或ancestorfemale)。
struct name
is for the member name and gender for whom we have to take ancestors and point the *ptr_ancestor
to that corresponding ancestor array (ancestormale or ancestorfemale).
在内存中的对象是工会。好。我的节目将具有工会的阵列,事实上。阵列的每个元件可以是在联合使用不同的结构。
在这里,我们要小心在分配指针,否则我们可能会在运行时失去我们的年长者的记录。
The object in memory is a union. Ok. My program will have an array of unions, in fact. Each element of the array may be using a different structure in the union. Here we should be careful in assigning the pointer or else we may loose our older person records at run time.
如果可能的话请告诉我怎么去1元即细节。 原[0]
即使考虑后原[1]
。在这里,我刚开数组的最后一个元素,所有的previous记录在运行时都没有了。我没有使用任何其他数据结构或文件。
If possible please tell me how to get the details of 1st element ie. original[0]
even after taking the original[1]
. Here I am just getting the last element of the array, and all previous records are gone at run time. I am not using any other data structures or files.
我的环境是在Windows上的Turbo C。
My environment is Turbo C on Windows.
推荐答案
您需要阅读有关工会这个问题。你想要的东西更像是:
You need to read this question about unions. You want something more like:
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;
};
}
,那么你可以测试family.names.gender和family.names.married来确定使用哪个成员的工会。
then you can test family.names.gender and family.names.married to determine which member of the union to use.
这篇关于为什么这个工会删除第一个记录数组在c code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!