이 Union이 C 코드의 배열에서 첫 번째 레코드를 삭제하는 이유는 무엇입니까?

StackOverflow https://stackoverflow.com/questions/252644

  •  05-07-2019
  •  | 
  •  

문제

다음은 4 개의 다른 구조가있는 Union 템플릿으로 구성된 헤더 파일 중 하나입니다.

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

저의 목표는 사람의 이름과 성별을 얻고 다음과 같이 사람의 성별과 결혼 상태에 따라 사람의 남성/여성 조상을 저장하는 것입니다.

내 말은 MAX 3 명의 회원이 있고 각자는 3 명의 조상을 가질 것입니다. 이 조상은 다음 조건과 같이 해당 구성원의 성별에 의해 결정됩니다.

  • 남성이 사용하는 경우 struct male
  • 여성 미혼을 사용하는 경우 struct unmarry
  • 여성이 결혼 한 경우 struct marry

struct name 우리가 조상을 취하고 지적 해야하는 회원 이름과 성별을위한 것입니다. *ptr_ancestor 해당 조상 어레이 (조상 또는 조상)에.

메모리의 대상은 노조입니다. 확인. 내 프로그램에는 실제로 조합이 있습니다. 배열의 각 요소는 유니온에서 다른 구조를 사용하고있을 수 있습니다. 여기서 우리는 포인터를 할당하는 데주의를 기울여야합니다. 그렇지 않으면 런 타임에 노인 기록을 잃을 수 있습니다.

가능하면 첫 번째 요소의 세부 사항을 얻는 방법을 알려주십시오. original[0] 복용 한 후에도 original[1]. 여기서는 배열의 마지막 요소를 얻고 있으며 모든 이전 레코드는 실행 시간에 사라졌습니다. 다른 데이터 구조 나 파일을 사용하지 않습니다.

내 환경은 Windows의 Turbo 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;
    };
}

그런 다음 family.names.gender 및 family.names.married를 테스트하여 사용할 수있는 노조의 구성원을 결정할 수 있습니다.

다른 팁

당신은 노조의 목적을 오해하고있을 수 있습니다.

노동 조합은 일반적으로 저장하는 데 사용됩니다 하나 여러 형태 중 하나 일 수있는 항목. 예를 들어:

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

a. 사이의 중요한 차이 struct 그리고 a union 그게 a입니다 struct 많은 데이터의 집계이지만 union 이다 오버레이: 당신은 만 보관할 수 있습니다 하나 요소는 모두 동일한 메모리를 공유하기 때문에. 위의 예에서 employees[] 배열은 10 바이트로 구성되며, 이는 유지할 수있는 가장 작은 양의 메모리입니다. 어느 하나 10 char에스 또는 1 int. 당신이 참조하면 name 요소, 10을 저장할 수 있습니다 char에스. 당신이 참조하면 serialNum 요소, 1을 저장할 수 있습니다 int (4 바이트) 및 나머지 6 바이트에 액세스 할 수 없습니다.

그래서 나는 당신이 가족을 대표하기 위해 다른 별도의 구조를 사용하고 싶다고 생각합니다. 당신이 한 일은 여러 정사각형 페그를 한 둥근 숙제 과제에 넣는 것으로 보입니다. :-)

고급 독자의 참고 사항 : 패딩 및 단어 정렬을 언급하지 마십시오. 그들은 아마도 다음 학기에 다루었을 것입니다. :-)

C에서 노동 조합이 무엇을 의미하는지 아십니까? 당신의 노조에는 3 명의 회원이 없습니다. 당신의 노조에는 4 명의 회원이 있습니다. 그 4 명의 회원 중에서, 당신은 몇 명을 보관하고 싶습니까?

왜 TA에게 물어 보지 않았습니까?

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top