목 차
안녕하세요.
오늘은 C언어의 구조체에 대해 알아보도록 하겠습니다.
구조체
여러 변수들을 하나로 뭉쳐 관리가 편하게 접근할 수 있게 하는 것이 구조체입니다.
형식
struct 구조체명 { 데이터타입 변수명1; 데이터타입 변수명2; : : } 구조체변수명; |
예제
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#include <stdio.h>
#include <string.h>
#pragma warning(disable: 4996) // VS2019 일떄 필요합니다.
void main()
{
struct Student
{
char sName[10];
int nAge;
char sAddress[32];
}student;
strcpy(student.sName,"AAAAA");
student.nAge = 10;
strcpy(student.sAddress,"Seoul, Republic of Korea");
printf("student.sName : %s\n", student.sName);
printf("student.nAge : %d\n", student.nAge);
printf("student.sAddress : %s\n", student.sAddress);
}
|
cs |
구조체의 필요성
만약에 다음과 같은 학생 정보에 대한 데이터가 있는 코드가 있습니다.
char sName[5][10]; int nAge[5]; char sAddress[5][32]; |
구조체를 사용하지 않아도, 배열로 데이터를 관리할 수 있습니다.
프로그램을 유지보수하게 되어 학생정보가 아닌 선생님정보가 추가해야하거나
교직원 정보를 추가해야한다면 다음과 같이 코드를 만들어야 할 것 입니다.
char sStudentName[5][10]; int nStudentAge[5]; char sStudentAddress[5][32]; char sTeacherName[5][10]; int nTeacherAge[5]; char sTeacherAddress[5][32]; char sSchoolstaffName[5][10]; int nSchoolstaffAge[5]; char sSchoolstaffAddress[5][32]; |
이렇게 되면 가독성이 현저히 떨어지기 때문에 구조체로 관리하는게 조금 더 수월하게 관리할 수 있을 것 입니다.
typedef
보통 구조체를 정의하고 사용하려면 다음과 같이 코드를 작성해야합니다.
struct Student { char sName[10]; int nAge; char sAddress[32]; }; struct Student student; |
struct가 구조체란 뜻이 되므로 struct를 구조체이름과 같이 붙여야 구조체를 선언할 수 있습니다.
typedef를 사용하면 struct를 붙이지 않고 구조체를 선언할 수 있습니다.
다음과 같이 정의합니다.
typedef struct Student { char sName[10]; int nAge; char sAddress[32]; }STUDENT; STUDENT student; |
이때 STUDENT은 구조체 데이터가 아니라 struct Student를 대신하는 이름이 됩니다.
구조체 멤버 참조
구조체 멤버 참조는 "."(도트)연산자를 이용해서 참조합니다.
구조체 포인터
포인터 변수를 이용해서 구조체를 포인터로 접근할 수 있습니다.
포인터 변수를 참조할 때에는 "->"(에로우)연산자를 이용해서 참조합니다.
다음과 같이 참조합니다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
#include <stdio.h>
#include <string.h>
#pragma warning(disable: 4996) // VS2019 일떄 필요합니다.
void main()
{
typedef struct Student
{
char sName[10];
int nAge;
char sAddress[32];
}STUDENT;
STUDENT student;
STUDENT* pStudent = &student;
strcpy(pStudent->sName, "AAAAA");
student.nAge = 10;
strcpy(student.sAddress, "Seoul, Republic of Korea");
printf("student.sName : %s\n", pStudent->sName);
printf("student.nAge : %d\n", pStudent->nAge);
printf("student.sAddress : %s\n", pStudent->sAddress);
}
|
cs |
pStudent->sName 대신 (*pStudent).sName를 사용할 수도 있습니다.
자기참조 구조체
구조체 멤버변수는 어떠한 타입도 가능하다. 다른 구조체 타입도 가능하다.
자기 자신은 포인트 형태로 가질 수 있다.
typedef struct Student { char sName[10]; int nAge; char sAddress[32]; struct Student* stu; }STUDENT; |
구조체의 메모리 배치
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#include <stdio.h>
#include <string.h>
void main()
{
typedef struct Player
{
char sName[3];
int nJob;
char chType;
int nAge;
short shPoint;
}PLAYER;
PLAYER student;
printf("student size : %d\n", sizeof(PLAYER));
}
|
cs |
다음과 같이 구조체의 사이즈를 확인해보면 20이 나옵니다.
데이터 타입으로 계산하면 sName(3) + nJob(4) + chType(1) + nAge(4) + shPoint(2) = 14가 나와야하는데 이상합니다.
구조체 변수의 메모리 배치는 4의 배수 단위로 배치됨으로 인해
sName(4) + nJob(4) + chType(4) + nAge(4) + shPoint(4) = 20 으로 계산이 됩니다.
그러므로 다음과 같이 수정해야 구조체 사이즈를 줄일 수 있습니다.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#include <stdio.h>
#include <string.h>
void main()
{
typedef struct Player
{
char sName[3];
char chType;
short shPoint;
int Job;
int nAge;
}PLAYER;
PLAYER student;
printf("student size : %d\n", sizeof(PLAYER));
}
|
cs |
이렇게 구현하면 sName(3) + nJob(1) + shPoint(4) + chType(4) + nAge(4) = 16 됩니다.
작은 사이즈의 변수타입부터 넣고 큰 사이즈 변수를 넣는게 사이즈를 줄일 수 있는 방법입니다.
'개발공부 > C언어&C++' 카테고리의 다른 글
[C언어&C++] strcmp, strncmp 함수(문자열 비교 함수) (0) | 2020.03.03 |
---|---|
[C언어&C++] strcpy, strncpy 함수(문자열 복사 함수) (0) | 2020.03.02 |
[C언어 강좌] 제 10강 함수( function ) (0) | 2020.02.28 |
[C언어 강좌] 제 9강 포인터( 포인터변수 ) (0) | 2020.02.27 |
[C언어 강좌] 제 8강 반복문( for, while, do-while ) (0) | 2020.02.26 |