티스토리 뷰
반응형
1) 목표
- 구조체(struct) 이해하기
- 구조체 사용법 이해하기
2) 설명
- 구조체는 여러 타입을 묶어서 사용하는 자료구조형입니다.
// struct 정의
struct tag {
char member1;
int member2;
double member3;
};
// struct 선언
struct tag myTag;
// typedef를 사용한 struct 정의
// MY_TAG를 새로운 타입으로 생성
typedef struct {
char member1;
int member2;
double member3;
} MY_TAG;
// struct 선언
MY_TAG myTag;
- 구조체의 정의는
struct (키워드) tag(태그명) { 사용하고자 하는 타입들 };
태그명은 여러분들이 알맞은 이름을 사용하시면 됩니다.
주식의 현재를 예를 들면 주식의 코드, 주식명, 현재가, 시가, 고가, 저가, 거래량 으로 정의 될수 있습니다.
// 주식의 현재가
struct _tagCurrentPrice {
char code[10]; // 주식코드
char name[80]; // 주식명
int currentPrice; // 현재가
int startPrice; // 시가
int highPrice; // 고가
int lowPrice; // 저가
double qty; // 거래량
};
- 태그명은 _tagCurrentPrice로 지정했고, 현재가라는 구조체가 관리(처리)하는 데이타는 code, name, currentPrice, startPrice, highPrice, lowPrice, qty 라는 멤버로 정의 합니다.
- 현재가 구조체를 사용하고자 하면
struct _tagCurrentPrice CurrentPrice_st;
- 이렇게 선언을 해서 사용합니다. c언어에서 제공하는 타입을 선언할때 타입 변수명; 으로 선언을 했듯이 구조체도 struct _tagCurrentPrice 타입으로 CurrentPrice_st라는 변수명으로 선언을 합니다.
※ 선언을 할때만 메모리에 할당이 됩니다. 위에서 현재가 구조체를 정의할때는 메모리에 할당되지 않습니다. - 현재가 구조체의 멤버 (code, name, currentPrice, startPrice, highPrice, lowPrice, qty)에 접근할때는
CurrentPrice_st.code, CurrentPrice_st.name, CurrentPrice_st.currentPrice, CurrentPrice_st.startPrice, CurrentPrice_st.highPrice, CurrentPrice_st.lowPrice, CurrentPrice_st.qty 로
구조체 변수명에 . 를 붙이고 멤버명을 써 주시면 해당하는 멤버에 접근 할 수 있습니다.
3-1) 예제 프로그램
▶ vi struct.c
#include <stdio.h>
#include <string.h>
// 주식 현재가 struct 정의
struct _tagCurrentPrice {
char code[10]; // 주식코드
char name[80]; // 주식명
int currentPrice; // 현재가
int startPrice; // 시가
int highPrice; // 고가
int lowPrice; // 저가
double qty; // 거래량
};
void main() {
// struct _tagCurrentPrice의 선언
struct _tagCurrentPrice currentPrice_st;
// 멤버 주식코드 값을 설정
memset(currentPrice_st.code, 0x00, sizeof(currentPrice_st.code));
strcpy(currentPrice_st.code, "A005930");
// 멤버 주식명 값을 설정
memset(currentPrice_st.name, 0x00, sizeof(currentPrice_st.name));
strcpy(currentPrice_st.name, "삼성전자");
// 멤버 현재가 값을 설정
currentPrice_st.currentPrice = 61000;
// 멤버 시가 값을 설정
currentPrice_st.startPrice = 60500;
// 멤버 고가 값을 설정
currentPrice_st.highPrice = 62000;
// 멤버 저가 값을 설정
currentPrice_st.lowPrice = 60300;
// 멤버 거래량 값을 설정
currentPrice_st.qty = 1234500.0;
// 값 출력
printf("주식코드:[%s]\n", currentPrice_st.code);
printf("주식명 :[%s]\n", currentPrice_st.name);
printf("현재가 :[%d]\n", currentPrice_st.currentPrice);
printf("시가 :[%d]\n", currentPrice_st.startPrice);
printf("고가 :[%d]\n", currentPrice_st.highPrice);
printf("저가 :[%d]\n", currentPrice_st.lowPrice);
printf("거래량 :[%.0f]\n", currentPrice_st.qty);
}
▶ 컴파일/실행
~/c-lecture (master ✘)✭ ᐅ gcc -o struct struct.c
~/c-lecture (master ✘)✭ ᐅ ./struct
주식코드:[A005930]
주식명 :[삼성전자]
현재가 :[61000]
시가 :[60500]
고가 :[62000]
저가 :[60300]
거래량 :[1234500]
~/c-lecture (master ✘)✭ ᐅ
▶ 분석
- 구조체를 struct _tagCurrentPrice 로 정의를 했고, struct _tagCurrentPrice currentPrice_st 로 선언을 했습니다.
- 구조체의 멤버에 값을 할당합니다.
char code[10], char name[80] 은 문자열로 값을 할당하는데 memset()로 초기화를 하고 strcpy()를 사용을 해서 값을 설정합니다.
strcpy(currentPrice_st.code, "A005930");
strcpy(currentPrice_st.name, "삼성전자");
struct 변수 . 멤버이름으로 접근합니다. - struct도 선언과 동시에 초기값을 설정할 수 있습니다.
struct _tagCurrentPrice currentPrice_st = {"A005930", "삼성전자", 61000, 60500, 62000, 60300, 1234500.0}; - printf()함수로 멤버를 출력합니다.
3-2) 값대입 예제 프로그램
▶ vi struct2.c
#include <stdio.h>
#include <string.h>
// 주식 현재가 struct 정의
struct _tagCurrentPrice {
char code[10]; // 주식코드
char name[80]; // 주식명
int currentPrice; // 현재가
int startPrice; // 시가
int highPrice; // 고가
int lowPrice; // 저가
double qty; // 거래량
};
void main() {
// struct _tagCurrentPrice의 선언
struct _tagCurrentPrice currentPrice_st = {"A005930", "삼성전자", 61000, 60500, 62000, 60300, 1234500.0};
struct _tagCurrentPrice tmpPrice_st;
// 값 출력
printf("--- currentPrice_st 출력 ---\n");
printf("주식코드:[%s]\n", currentPrice_st.code);
printf("주식명 :[%s]\n", currentPrice_st.name);
printf("현재가 :[%d]\n", currentPrice_st.currentPrice);
printf("시가 :[%d]\n", currentPrice_st.startPrice);
printf("고가 :[%d]\n", currentPrice_st.highPrice);
printf("저가 :[%d]\n", currentPrice_st.lowPrice);
printf("거래량 :[%.0f]\n", currentPrice_st.qty);
// struct 값 대입
tmpPrice_st = currentPrice_st;
printf("--- tmpPrice_st 출력 ---\n");
printf("주식코드:[%s]\n", tmpPrice_st.code);
printf("주식명 :[%s]\n", tmpPrice_st.name);
printf("현재가 :[%d]\n", tmpPrice_st.currentPrice);
printf("시가 :[%d]\n", tmpPrice_st.startPrice);
printf("고가 :[%d]\n", tmpPrice_st.highPrice);
printf("저가 :[%d]\n", tmpPrice_st.lowPrice);
printf("거래량 :[%.0f]\n", tmpPrice_st.qty);
}
▶ 컴파일/실행
~/c-lecture (master ✘)✭ ᐅ gcc -o struct2 struct2.c
~/c-lecture (master ✘)✭ ᐅ ./struct2
--- currentPrice_st 출력 ---
주식코드:[A005930]
주식명 :[삼성전자]
현재가 :[61000]
시가 :[60500]
고가 :[62000]
저가 :[60300]
거래량 :[1234500]
--- tmpPrice_st 출력 ---
주식코드:[A005930]
주식명 :[삼성전자]
현재가 :[61000]
시가 :[60500]
고가 :[62000]
저가 :[60300]
거래량 :[1234500]
~/c-lecture (master ✘)✭ ᐅ
▶ 분석
- currentPrice_st 선언과 동시에 값을 할당합니다.
struct _tagCurrentPrice currentPrice_st = {"A005930", "삼성전자", 61000, 60500, 62000, 60300, 1234500.0}; - struct _tagCurrentPrice 의 다른 변수 tmpPrice_st를 선언합니다.
- 같은 struct인 경우 = 대입 연산자로 값을 할당할 수 있습니다.
tmpPrice_st = currentPrice_st; - 출력을 보면 값이 같은걸 확인하실 수 있습니다.
3-3) typedef문 예제 프로그램
▶ vi struct3.c
#include <stdio.h>
#include <string.h>
// 주식 현재가 struct 의 새로운 타입을 정의
typedef struct {
char code[10]; // 주식코드
char name[80]; // 주식명
int currentPrice; // 현재가
int startPrice; // 시가
int highPrice; // 고가
int lowPrice; // 저가
double qty; // 거래량
} CURRENT_PRICE_ST;
void main() {
// struct _tagCurrentPrice의 선언
CURRENT_PRICE_ST currentPrice_st = {"A005930", "삼성전자", 61000, 60500, 62000, 60300, 1234500.0};
// 값 출력
printf("주식코드:[%s]\n", currentPrice_st.code);
printf("주식명 :[%s]\n", currentPrice_st.name);
printf("현재가 :[%d]\n", currentPrice_st.currentPrice);
printf("시가 :[%d]\n", currentPrice_st.startPrice);
printf("고가 :[%d]\n", currentPrice_st.highPrice);
printf("저가 :[%d]\n", currentPrice_st.lowPrice);
printf("거래량 :[%.0f]\n", currentPrice_st.qty);
}
▶ 컴파일/실행
~/c-lecture (master ✘)✭ ᐅ gcc -o struct2 struct2.c
~/c-lecture (master ✘)✭ ᐅ ./struct2
주식코드:[A005930]
주식명 :[삼성전자]
현재가 :[61000]
시가 :[60500]
고가 :[62000]
저가 :[60300]
거래량 :[1234500]
~/c-lecture (master ✘)✭ ᐅ
▶ 분석
- typedef 문을 사용해서 struct 태그명을 사용하기 쉬운 별칭으로 정의할 수 있습니다.
struct _tagCurrentPrice 를 typedef struct ~~ CURRENT_PRICE_ST 로 별칭을 새롭게 정의하였습니다.
구조체를 선언할때 이 별칭으로 사용됩니다.
CURRENT_PRICE_ST currentPrice_st;
struct _tagCurrentPrice로 사용할때 보다 간결하게 사용가능합니다.
보통 개발할때 구조체는 typedef로 별칭을 새롭게 정의해서 사용합니다. - 선언과 동시에 초기값을 설정해 보았습니다.
CURRENT_PRICE_ST currentPrice_st = {"A005930", "삼성전자", 61000, 60500, 62000, 60300, 1234500.0};
반응형
'리눅스 C-언어 초급과정' 카테고리의 다른 글
18. 구조체 중첩 (0) | 2022.11.21 |
---|---|
17. 구조체와 포인터 (0) | 2022.11.21 |
15. 문자열(배열) 다루기 (0) | 2022.11.17 |
14. 함수 파라메타 유형 (0) | 2022.11.17 |
14. 함수 (0) | 2022.11.16 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- c프로그램
- 리눅스
- memcpy
- Pointer
- struct
- sharetravelplan
- #include
- array
- 파라메타
- 배열
- 포인터
- #ifdef
- 문자열
- IPC
- 전처리기
- Call-By-Reference
- #define
- strcpy
- C언어
- Member
- GCC
- String
- While
- Linux
- Call-by-value
- memset
- 의유
- 재고
- 소켓
- Clang
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
글 보관함