리눅스 C-언어 초급과정
18. 구조체 중첩
다유데브
2022. 11. 21. 16:24
반응형
1) 목표
- 구조체안의 구조체 이해하기
2) 설명
// 값만을 가지는 struct
typedef struct {
int currentPrice; // 현재가
int startPrice; // 시가
int highPrice; // 고가
int lowPrice; // 저가
} PRICE_ST;
// 주식 현재가 struct 의 새로운 타입을 정의
// struct의 멤버로 struct를 가질수 있습니다.
typedef struct {
char code[10]; // 주식코드
char name[80]; // 주식명
PRICE_ST price;
double qty; // 거래량
} CURRENT_PRICE_ST;
- 구조체의 멤버로 다른 구조체가 될 수 있습니다.
- CURRENT_PRICE_ST안에 다른 구조체 PRICE_ST 가 멤버가 될 수 있습니다.
3) 예제 프로그램
▶ vi struct8.c
#include <stdio.h>
#pragma pack(1)
// 값만을 가지는 struct
typedef struct {
int currentPrice; // 현재가
int startPrice; // 시가
int highPrice; // 고가
int lowPrice; // 저가
} PRICE_ST;
// 주식 현재가 struct 의 새로운 타입을 정의
// struct의 멤버로 struct를 가질수 있습니다.
typedef struct {
char code[10]; // 주식코드
char name[80]; // 주식명
PRICE_ST price;
double qty; // 거래량
} CURRENT_PRICE_ST;
// call-by-value로 구조체 사용하기
void print_value(CURRENT_PRICE_ST valuePrice) {
printf("--- print_value ---\n");
printf("주식코드:[%s]\n", valuePrice.code);
printf("주식명 :[%s]\n", valuePrice.name);
printf("현재가 :[%d]\n", valuePrice.price.currentPrice);
printf("시가 :[%d]\n", valuePrice.price.startPrice);
printf("고가 :[%d]\n", valuePrice.price.highPrice);
printf("저가 :[%d]\n", valuePrice.price.lowPrice);
printf("거래량 :[%.0f]\n", valuePrice.qty);
}
// call-by-reference로 구조체 사용하기
void print_reference(CURRENT_PRICE_ST *ptrPrice) {
printf("--- print_reference ---\n");
printf("주식코드:[%s]\n", ptrPrice->code);
printf("주식명 :[%s]\n", ptrPrice->name);
printf("현재가 :[%d]\n", ptrPrice->price.currentPrice);
printf("시가 :[%d]\n", ptrPrice->price.startPrice);
printf("고가 :[%d]\n", ptrPrice->price.highPrice);
printf("저가 :[%d]\n", ptrPrice->price.lowPrice);
printf("거래량 :[%.0f]\n", ptrPrice->qty);
}
void main() {
// struct _tagCurrentPrice의 선언
CURRENT_PRICE_ST currentPrice_st = {"A005930", "삼성전자", {61000, 60500, 62000, 60300}, 1234500.0};
// 포인터 변수
CURRENT_PRICE_ST *ptrPrice_st = ¤tPrice_st;
// call-by-value 값 출력
print_value(currentPrice_st);
// call-by-reference 값 출력
print_reference(ptrPrice_st);
}
▶ 컴파일/실행
~/c-lecture (master ✘)✭ ᐅ gcc -o struct8 struct8.c
~/c-lecture (master ✘)✭ ᐅ ./struct8
--- print_value ---
주식코드:[A005930]
주식명 :[삼성전자]
현재가 :[61000]
시가 :[60500]
고가 :[62000]
저가 :[60300]
거래량 :[1234500]
--- print_reference ---
주식코드:[A005930]
주식명 :[삼성전자]
현재가 :[61000]
시가 :[60500]
고가 :[62000]
저가 :[60300]
거래량 :[1234500]
~/c-lecture (master ✘)✭ ᐅ
▶ 분석
- CURRENT_PRICE_ST 안에 PRICE_ST 를 멤버로 사용할 수 있습니다.
- 선언과 동시에 값을 할당할때 구조체는 {} 로 초기화하는데 구조체안의 구조체는 { {} }로 초기화합니다.
- call-by-value에서 PRICE_ST 멤버의 접근은 구조체 멤버의 접근과 같습니다.
...
CURRENT_PRICE_ST currentPrice_st = {"A005930", "삼성전자", {61000, 60500, 62000, 60300}, 1234500.0};
currentPrice_st의 멤버 price는 구조체이므로 다시 {}를 사용해서 {61000, 60500, 62000, 60300}로 초기화합니다.
...
printf("현재가 :[%d]\n", valuePrice.price.currentPrice);
printf("시가 :[%d]\n", valuePrice.price.startPrice);
printf("고가 :[%d]\n", valuePrice.price.highPrice);
printf("저가 :[%d]\n", valuePrice.price.lowPrice);
- call-by-reference의 포인터로써의 접근은 ptrPrice포인터 변수의 -> price의 멤버는 . 로 접근가능합니다.
...
printf("현재가 :[%d]\n", ptrPrice->price.currentPrice);
printf("시가 :[%d]\n", ptrPrice->price.startPrice);
printf("고가 :[%d]\n", ptrPrice->price.highPrice);
printf("저가 :[%d]\n", ptrPrice->price.lowPrice);
다른 멤버처럼 PRICE_ST의 price는 -> 로 접근가능합니다.
ptrPrice->price 여기에 price는 구조체이므로 구조체의 멤버로 접근하는 . 멤버명을 사용합니다.
ptrPrice->price.currentPrice
반응형