1.구조체 정의
영어로 Structure라고 하며 여러 개의 데이터 집합에 이름을 부여한 것임..
struct GameData
{
int n_index;
char szName[40];
double dbData;
};
즉 다음과 같은 n_index, szName, dbData 같은 자료를 GameData라는 구조로
묶어놓기 위해 만든것..
그렇다면
구조체(Structure)의 멤버의 자료형에는 어떤것이 올까..
2. 구조체의 자료형
멤버로 오는 자료형은 무엇이든 상관없다..
struct ThisGame
{
struct GameData Charactor;
struct GameData MapInfo;
struct GameData UserInfo;
char FunnyGame[50];
};
ThisGame이라는 것은 GameData를 모아놓은 또 하나의 구조체다..
이와 같이 자료를 묶어놓고 관리하게 편하게 만들기 위해 구조체를 사용한다.
3. 구조체의 용도
그렇다면 왜 굳이 구조체를 써야할까..
프로그램이란 함수와 변수의 집합으로 이루어 지기 때문이다.
만약 구조체라는 개념이 없다면 어떻게 될까..
int n_index;
char szName[40];
double dbData;
이와 같은 변수들을 여러개 선언하여 쓰고 싶다고 가정한다면
구조체가 없다면 n_index[100], szName[100][40], double dbData[100],
이런식으로 써야할 것이다.. 그리고 코드상 알아보기도 상당히 난해하다..
구조체를 사용한다면 단순히
GameData DataStucture[100];
하면 해결될 일이다...
그리고 우리가 생각하고 있는 것들을 표현해 내기에도 적합한게
데이터의 구조(Stucture)화 이다.
4. 구조체 사용방법
구조체의 선언과 멤버 사용에 대해 알아본다.
일단 구조체를 정의하는 방법은 위에 나와있는데로 하면된다.
#include <stdio.h>
struct GameData
{
int n_index;
char szName[40];
double dbData;
};
struct ThisGame
{
struct GameData Charactor;
struct GameData MapInfo;
struct GameData UserInfo;
char FunnyGame[50];
};
int main()
{
struct ThisGame GameInformation;
GameInformation.Charactor.n_index = 1;
strcpy(GameInformation.Charactor.szName, "SCV");
GameInformation.Charactor.dbData = 0.001;
GameInformation.MapInfo.n_index = 1;
.......
.....
..
return 0;
}
또한 구조체끼리 데이터를 대입시킬수도 있다.
struct GameData SourceData, TargetData;
SourceData.n_index = 0;
strcpy(SourceData.szName, "test");
SourceData.dbData = 1.2938;
TargetData = SourceData; // 구조체 복사
'Programming' 카테고리의 다른 글
| ID3DXSprite::Begin (0) | 2007/10/02 |
|---|---|
| D3DXCreateSprite (0) | 2007/09/30 |
| 연결 리스트 ( Linked List ) 링크드 리스트 (0) | 2007/09/23 |
| 순열(next_permutation)과 문자열 변환 (0) | 2007/09/16 |
| next_permutation (0) | 2007/09/13 |
| DICamera (0) | 2007/06/19 |
| Camera Class 구현 (0) | 2007/06/19 |
| DirectX Study (3) (0) | 2007/06/19 |
| DirectX Study (2) (0) | 2007/06/18 |
| DirectX Study (1) (0) | 2007/06/18 |
| 구조체 (0) | 2007/06/16 |






댓글을 달아 주세요