2013-04-12 2 views
-1
/* Example in unions */ 

#include <stdio.h> // standard header file 
#include <ctype.h> // for atoi function 
#include <stdlib.h> // for malloc function 
#include <assert.h> // for assert macro 

#define MAX 512  // for fgets() 

typedef enum {National, Alien} PERSON_KIND; 

typedef struct date { 
    int month; 
    int day; 
    int year; 
} DATE; 

typedef struct { 
    char countryOfOrigin[30]; 
    DATE * dateOfEntry; // so you get comfortable with pointers 
    enum { Worker, Tourist, Student} visaStatus; 
    char portOfEntry[30]; 
} ALIEN; 

typedef struct { 
    char placeOfBirth[30]; 
    char ssn[20]; 
} CITIZEN; 

typedef union { 
    ALIEN alien; 
    CITIZEN citizen; 
} PERSON_INFO; 

typedef struct person { 
    char name[30]; 
    DATE * dateOfBirth; // just to make it interesting 
    PERSON_KIND personKind; 
    PERSON_INFO info; 
} PERSON; 

void fillInfo(PERSON *); 
void printInfo(const PERSON *); 

int main() { 


    printf("sizeof(int) is %u\n", (unsigned) sizeof(int)); 
    printf("sizeof(PERSON_KIND) is %u\n", (unsigned) sizeof(PERSON_KIND)); 
    printf("sizeof(DATE) is %u\n", (unsigned) sizeof(DATE)); 
    printf("sizeof(ALIEN) is %u\n", (unsigned) sizeof(ALIEN)); 
    printf("sizeof(CITIZEN) is %u\n", (unsigned) sizeof(CITIZEN)); 
    printf("sizeof(PERSON_INFO) is %u\n", (unsigned) sizeof(PERSON_INFO)); 
    printf("sizeof(PERSON) is %u\n", (unsigned) sizeof(PERSON)); 

    /* fillInfo(&p1); 
    printf("\nThe infomation you entered is given below:\n"); 
    printInfo(&p1); 

    fillInfo(&p2); 
    printf("\nThe infomation you entered is given below:\n"); 
    printInfo(&p2); */ 

    return 0; 
} 

/* void fillInfo(PERSON *p) { 
    char buf[MAX], i; 



    printf("Please enter the person's name: "); 


    printf("Please enter the birth month: "); 

    printf("Please enter the birth date: "); 

    printf("Please enter the birth year: "); 

    printf("Please enter 1 if the person is a National or 2\n"); 
    printf("if the person is an alien: "); 
    // get the value from the user and store it as an integer in i 

    if (i == 1) p -> personKind = National; 
    else p -> personKind = Alien; 

    if (p -> personKind == National) { 
     printf("Please enter the place of birth: "); 

     printf("Please enter the ssn: "); 

    } 

    if (p -> personKind == Alien) { 
     printf("Please enter the country of origin: "); 

     printf("Please enter the month the person entered this country: "); 


     printf("Please enter the day of the month the person entered this country: "); 


     printf("Please enter the year the person entered this country: "); 

     printf("Please enter the person's immigration status : \n"); 
     printf("\t1 for Worker:\n"); 
     printf("\t2 for Tourist:\n"); 
     printf("\t3 for Student: \n"); 
     fgets(buf, MAX, stdin); 
     i = atoi(buf); 

     switch (i) { 
      case 1 : 
       p -> info.alien.visaStatus = Worker; 
       break; 
      case 2 : 
       p -> info.alien.visaStatus = Tourist; 
       break; 
      case 3 : 
       p -> info.alien.visaStatus = Student; 
     } 

     printf("Please enter the port of entry: "); 

    } 
} 

void printInfo(const PERSON *p) { 
    printf("Name:   %s\n", p -> name); 

    printf("Date of Birth: %2d/%2d/%4d\n",); 

    printf("Status: "); 
    if (p -> personKind == National) printf("National\n"); 
    else printf("Alien\n"); 

    if (p -> personKind == National) { 
     printf("Place of birth: %s\n",); 
     printf("SSN: %s\n",); 
    } 

    if (p -> personKind == Alien) { 
     printf("Country of origin: %s\n",); 

     printf("Date of Entry: %2d/%2d/%4d\n", ); 

     printf("Immigration status: "); 

     switch (p -> info.alien.visaStatus) { 
      case Worker : 
       printf("Worker\n"); 
       break; 
      case Tourist : 
       printf("Tourist\n"); 
       break; 
      case Student : 
       printf("Student\n"); 
     } 
     printf("Port of Entry: %s\n\n",); 
    } 
} // print_info */ 

예상 출력 : 모든 주석 코드가 주석입니다

sizeof(int) is 4 
sizeof(PERSON_KIND) is 4 
sizeof(DATE) is 12 
sizeof(ALIEN) is 80 
sizeof(CITIZEN) is 50 
sizeof(PERSON_INFO) is 80 
sizeof(PERSON) is 128 
Please enter the person's name: John Doe 
Please enter the birth month: 10 
Please enter the birth date: 31 
Please enter the birth year: 1989 
Please enter 1 if the person is a National or 2 
if the person is an alien: 1 
Please enter the place of birth: Worcester 
Please enter the ssn:

The infomation you entered is given below: 
Name:   John Doe 

Date of Birth: 10/31/1989 
Status: National 
Place of birth: Worcester 

SSN:

Please enter the person's name: Jane Smith 
Please enter the birth month: 2 
Please enter the birth date: 20 
Please enter the birth year: 1970 
Please enter 1 if the person is a National or 2 
if the person is an alien: 2 
Please enter the country of origin: Libya 
Please enter the month the person entered this country: 10 
Please enter the day of the month the person entered this country: 23 
Please enter the year the person entered this country: 2009 
Please enter the person's immigration status : 
    1 for Worker: 
    2 for Tourist: 
    3 for Student: 
1 
Please enter the port of entry: New York 

The infomation you entered is given below: 
Name:   Jane Smith 

Date of Birth: 2/20/1970 
Status: Alien 
Country of origin: Libya 

Date of Entry: 10/23/2009 
Immigration status: Worker 
Port of Entry: New York 

컴파일러 오류 : 만 이대로가 ... 노동 조합에 익숙하지 않은 오전

Example_union.c: In function ‘main’: 
Example_union.c:56:12: error: ‘p1’ undeclared (first use in this function) 
Example_union.c:56:12: note: each undeclared identifier is reported only once for each function it appears in 
Example_union.c:60:12: error: ‘p2’ undeclared (first use in this function) 
Example_union.c:62:20: error: expected expression before ‘/’ token 
Example_union.c: In function ‘printInfo’: 
Example_union.c:132:41: error: expected expression before ‘)’ token 
Example_union.c:139:34: error: expected expression before ‘)’ token 
Example_union.c:140:23: error: expected expression before ‘)’ token 
Example_union.c:144:37: error: expected expression before ‘)’ token 
Example_union.c:146:43: error: expected expression before ‘)’ token 
Example_union.c:160:35: error: expected expression before ‘)’ token 
Example_union.c:65:1: warning: control reaches end of non-void function [-Wreturn-type] 

구조에 익숙하다. 위의 코드에서 올바른 결과를 내기 위해 코드/유니온 예제를 어떻게 끝낼 수 있을지 궁금합니다. 아직 완성되지 않은 코드의 현재 컴파일러 오류가 있습니다. 내가 절박한 필요가있을 때 도와 주길 바래. 감사.

+0

어디'p1' ??? 나를 찾아라! – gongzhitaao

+0

구조체에 익숙하지만 식별자를 사용하기 전에 식별자를 선언하지 않는다고 생각하십니까? – Kaz

+0

그게 요점이야 ... 완료해야합니다 –

답변

1

p1이 선언되지 않았지만 포인터 p1에 & p1 과 같은 포인터가 있습니다. p2와 같습니다. 먼저 수정하십시오.

+0

자주 묻는 오류 컴파일러가 전체 문서를 이해하려고 시도하지만 컨텍스트가 첫 번째 오류로 인해 엉망이되기 때문에 나중에 나오는 문구는 쓰레기입니다. – uchuugaka