C++ 구조체 포인터

2014.06.15 08:19

엘카 조회 수:926

참고 문서  
구조체 포인터란? 
     - 포인터 변수를 통하여 구조체 변수에 접근하겠다는 의미 
      - 구조체 포인터 변수도 일반 포인터 변수와 마찬가지로 변수명 앞에 '*'를 붙여 선언한다. 

구조체 포인터의 선언 형식 
struct 구조체 자료형명 { 
       자료형  멤버1; 
       자료형  멤버2; 
           ... 
       자료형  멤버N; 
} *변수명; 
 
또는 
 
struct 자료형명 *변수명; 

구조체 포인터 변수의 선언 예 
struct A { 
           char name[20]; 
           int  age; 
} st; //st는 일반 구조체 변수 
 
struct A  *ptr;  
ptr=&st; 
 
gets(ptr->name); 

구조체 포인터 변수를 사용하여 멤버에 접근할 때는 "->" 연산자를 사용한다. 


구조체 포인터 변수의 이해 
#include <stdio.h> 
 
int main(void) 
{   
   struct A { char name[20];       
              int age; } st;    
   struct A *ptr;   
 
   ptr=&st;   
   printf("st : %d, ptr : %d \n", sizeof(st), sizeof(ptr)); 
   printf("성명 ? ");   
   //gets(st.name);   
   gets(ptr->name);   
   printf("나이 ? ");   
   //scanf("%d", &st.age);   
   scanf("%d", &ptr->age);   
   printf("%s, %d \n", ptr->name, ptr->age);   
   return 0; 
} 

구조체 포인터 배열과 구조체 포인터 변수 
#include <stdio.h> 
 
int main(void) 
{   
   typedef struct member { 
      char name[20];     
      int age;  
   } MEMBER;   
   MEMBER person[30], *ptr;   
   int i, count;   
 
   ptr = person;        
   for(i=0; i<30; i++) {     
      printf("%d, 이름 ? (입력종료:end) ", i+1);  
      gets(ptr->name);     
      if(!strcmp(ptr->name,"end")) break;   
      printf("   나이 ? ");     
      scanf("%d%*c", &ptr->age);      
      ptr++;   
   }
 
 count = i;   
 
   printf("count  = %d  \n", count);   
   for(i=0; i< count; i++)     
      printf(" name : %s    age : %d \n", person[i].name, 
              person[i].age);    
   printf("\n"); 
   
   return 0; 
} 




중첩된 구조체(nested structure)란? 
 - 구조체가 다른 구조체를 포함하고 있는 형태 
  - 구조체 자료형에 다른 구조체 자료형이 하나의 멤버로 선언된 형태 

중첩된 구조체의 예 
   struct PERSONAL {  
                     char addr[64];     
                     char telno[16];     
                     int age;  
   };   
   struct MEMBER { 
                   int num;     
                   char name[20];     
                   struct PERSONAL info;  
   } employee;    



중첩된 구조체의 이해

#include <stdio.h> 
int main(void) {    
   struct PERSONAL {  
    char addr[64];         
                     char telno[16];       
                     int age; };   
   struct MEMBER {  
    int num;         
                   char name[20];       
                   struct PERSONAL info; } employee;    
   printf("구조체의 크기 : %d \n", sizeof(employee));   
   employee.num = 1;   
   printf("성명 ? "); gets(employee.name);   
   printf("주소 ? "); gets(employee.info.addr);   
   printf("전화번호 ? "); gets(employee.info.telno);   
   printf("나이 ? "); scanf("%d", &employee.info.age); 
   printf("%d, %s, %s, %s, %d \n", employee.num, employee.name, 
    employee.info.addr, employee.info.telno, employee.info.age);    
   return 0; 
} 


번호 제목 날짜 조회 수
» 구조체 포인터 2014.06.15 926
42 구조체의 선언과 데이터 입·출력 2014.06.15 3080
41 함수 포인터 2014.06.15 438
40 배열과 포인터 2014.06.15 402
39 포인터 연산 2014.06.15 383
38 포인터 변수 2014.06.15 411
37 데이터 정렬 2014.06.15 422
36 배열 초기화 2014.06.14 627
35 2차원 배열 2014.06.14 667
34 문자열 처리 함수 2014.06.14 407
33 문자열 입출력 함수 2014.06.14 435
32 접근 수정자 2014.06.14 819
31 전역 변수 2014.06.14 305
30 지역 변수(local variable) 2014.06.14 291
29 변수와 메모리 저장 위치 2014.06.14 423
28 비트 연산자 2014.06.14 284
27 관계 연산자와 논리 연산자 2014.06.14 467
26 증감 연산자 2014.06.14 316
25 printf() 함수의 형식 지정자 2014.06.14 7520
24 열거 형 변수의 이해 2014.06.14 465