Why this union is deleting the 1st records in arrays in the c code?

吃可爱长大的小学妹 提交于 2019-12-11 07:45:58

问题


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;

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..

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

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.

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.

My environment is Turbo C on Windows.


回答1:


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;
    };
}

then you can test family.names.gender and family.names.married to determine which member of the union to use.




回答2:


You may be misunderstanding the purpose of a union.

A union is typically used to store one item that may be in one of several forms. For example:

// 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");

The critical difference between a struct and a union is that a struct is an aggregate of many pieces of data, but a union is an overlayment: you may store only one of the elements because they all share the same memory. In the example above, each element in the employees[] array consists of 10 bytes, which is the smallest amount of memory that can hold either 10 chars or 1 int. If you reference the name element, you can store 10 chars. If you reference the serialNum element, you can store 1 int (say 4 bytes) and cannot access the remaining 6 bytes.

So I think you want to use different, separate structures to represent the family members. What you've done appears to be cramming several square pegs into one round homework assignment. :-)

Note for advanced readers: please don't mention padding and word alignment. They're probably covered next semester. :-)




回答3:


Do you know what union means in C? Your union doesn't have 3 members. Your union has 4 members. Among those 4 members, how many do you want to store values in?

Why didn't you ask your TA?



来源:https://stackoverflow.com/questions/252644/why-this-union-is-deleting-the-1st-records-in-arrays-in-the-c-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!