1. 구조체의 유용성과 필요성
구조체
구조체의 유용성
구조체의 필요성
2. 구조체의 정의
struct point // point라는 이름의 구조체 선언
{
int x; // 구조체 멤버 int x
int y; // 구조체 멤버 int y
};
int main(void){
struct point p1;
p1.x=10; // p1의 멤버 x에 10 대입
p1.y=20; // p1의 멤버 y에 20 대입
// ...
return 0;
};
사용자 정의 자료형
구조체 멤버
구조체 멤버 접근
.을 사용3. 구조체의 선언
정의와 동시에 선언
struct point
{
int x;
int y;
} p1,p2,p3;
정의, 선언 분리
struct point
{
int x;
int y;
};
int main(void){
struct point p1,p2,p3;
// ...
return 0;
}
4. 구조체 초기화
배열 초기화 문법
struct person{
char name[20];
char phone[20];
int age;
};
int main(void){
struct person p={"gildong","010-1234-5678", 21};
// ...
return 0;
}
멤버 지정 초기화
int main (void){
struct person p ={.age=21, .name="gildong", .phone="010-1234-5678"};
}
1. 구조체의 배열
구조체와 배열
#include <stdio.h>
struct person{
char name[20];
char phone[20];
};
int main(void){
struct person pArray[3]={
{"KIM","123"},
{"LEE","456"},
{"PARK","789"}
};
// ...
return 0;
}
2. 구조체 연산 & Return
구조체 변수의 연산
구조체 변수의 return
#include <stdio.h>
struct simple{int a,b;};
struct simple getdata(void)
// simple 구조체를 return하는 함수 getdata
{
//...
tmp.a=rand()%100, tmp.b=rand()%100;
return tmp;
}
int main(void){
struct simple s=getdata();
return 0;
}
1. typedef
개요
#include <stdio.h>
struct Data{
int data1;
int data2;
};
typedef struct Data Data; // 같은 이름 사용 가능
int main(void){
Data d= {1,2}; // struct 키워드 생략
return 0;
}
FILE, size_t2. 공용체
개요
union data{
int d1;
double d2;
char d3;
};
메모리 구조

3. enum
정의
enum color {RED=1, GREEN=3, BLUE=5};
enum color c;
c=RED; // c=1
c=GREEN; // c=3
c=BLUE; // c=5
활용
#include <stdio.h>
enum ele_type {TRI=3, TETRA=4, PENT=5};
typedef enum ele_typle ELE_TYPE;
int main(void){
enum ele_type e1=TRI;
ELE_TYPE e2=TETRA;
enum ele_type e3=PENT;
printf("%d %d %d\n",e1,e2,e3);
printf("%d %d %d\n",TRI,TETRA,PENT);
return 0;
}