โป ์ด ์นดํ ๊ณ ๋ฆฌ์ ๊ธ๋ค์ knu EK.Ryu ๊ต์๋์ <์๋ฃ๊ตฌ์กฐ> ์์ ์ ๋ฃ๊ณ ๋๋ฆ๋๋ก ํ์๊ฐ ์ ๋ฆฌํ ๊ธ์ ๋๋ค.
โป ๋ถ์กฑํ ์ค๋ช ์ด ์๊ฑฐ๋, ์๋ชป ์๊ณ ์์ฑํ ๋ถ๋ถ์ด ๋ณด์ธ๋ค๋ฉด ํธํ๊ฒ ๋๊ธ๋ก ์๋ ค์ฃผ์๋ฉด ์ ๋ง ๊ฐ์ฌํ๊ฒ ์ต๋๋ค๐

2.1 Arrays ๋ฐฐ์ด
๋ฐฐ์ด์ ์ธ๋ฑ์ค์ ๊ฐ <index, value>์ ์์ผ๋ก ๊ตฌ์ฑ๋ ์งํฉ์ผ๋ก์, ์ ์๋ ๊ฐ ์ธ๋ฑ์ค๋ ๊ทธ ์ธ๋ฑ์ค์ ๊ด๋ จ๋ ๊ฐ์ ๊ฐ์ง๋๋ค.
๋ฐฐ์ด์ ์ถ์ ๋ฐ์ดํ ํ์ (ADT)์ ์๋์ ๊ฐ์ต๋๋ค.
โปADT(์ถ์ ๋ฐ์ดํฐ ํ์ )๋? ๊ฐ์ฒด์ ๋ช ์ธ์ ๊ทธ ์ฐ์ฐ์ ๋ช ์ธ๊ฐ ๊ทธ ๊ฐ์ฒด์ ํํ๊ณผ ์ฐ์ฐ์ ๊ตฌํ์ผ๋ก๋ถํฐ ๋ถ๋ฆฌ๋ ๋ฐ์ดํฐ ํ์ ์ ๋๋ค. ์ฝ๊ฒ ๋งํด, ADT๋ ๋ฐ์ดํฐ์ ๊ทธ ๋ฐ์ดํฐ๋ฅผ ๋ค๋ฃจ๋ ์ฐ์ฐ์ ์ถ์์ ์ผ๋ก ์ ์ํ ๊ฒ์ผ๋ก, ๊ตฌํ ์ธ๋ถ ์ฌํญ์ ๊ตฌ์ ๋ฐ์ง ์๊ณ ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ฅผ ๋ค๋ฃฐ ์ ์๋ ๊ฐ๋ ์ ๋๋ค.

(1) C์ธ์ด์์์ ๋ฐฐ์ด
๋จผ์ 1์ฐจ์ ๋ฐฐ์ด๋ง์ ์๊ฐํด๋ด ์๋ค.
C์์ 1์ฐจ์ ๋ฐฐ์ด์ ๋ณ์์ ์ด๋ฆ ๋์ [ ] ์ ๊ฐ์ ์ค๊ดํธ๋ฅผ ์ถ๊ฐํ์ฌ ์ ์ธํฉ๋๋ค.
int list[5], *plist[5];
์์ ์ ์ธ์ ๊ฐ๊ฐ 5๊ฐ์ ์์๋ฅผ ํฌํจํ๋ ๋ ๋ฐฐ์ด์ ์ ์ธํ ๊ฒ์ ๋๋ค.
์ฒซ ๋ฒ์งธ ๋ฐฐ์ด์ 5๊ฐ์ ์ ์๋ฅผ ์ ์ํ ๋ฐ๋ฉด, ๋ ๋ฒ์งธ๋ ์ ์์ ๋ํ 5๊ฐ์ ํฌ์ธํฐ๋ฅผ ์ ์ํ์ต๋๋ค.
์ฆ, list[0], list[1], ... , list[4]๋ ๋ฐฐ์ด ์์์ ์ด๋ฆ์ด๊ณ , ์ด๋ค ๊ฐ์๋ ํ๋์ ์ ์ ๊ฐ(int ๊ฐ)์ ํฌํจํฉ๋๋ค.
๋ง์ฐฌ๊ฐ์ง๋ก plist[0], plist[1], ... ์ ์ ์์ ๋ํ ํฌ์ธํฐ๋ฅผ ํฌํจํฉ๋๋ค.
์์ ๋ฅผ ๋จผ์ ๋ด ์๋ค.
์์ 2.1 ๋ค์๊ณผ ๊ฐ์ด ๋ฐฐ์ด์ ์ ์ธํ ๊ฒฝ์ฐ๋ฅผ ๊ฐ์ ํด๋ด ์๋ค.
int one[] = {0, 1, 2, 3, 4};
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
void print1(int* ptr, int rows)
{
int i;
printf("Address Contents\n");
for (i = 0; i < rows; i++)
printf("%8u%5d\n", ptr + i, *(ptr + i));
printf("\n");
}
int main()
{
int one[] = {1, 2, 3, 4, 5};
int rows = 5;
print1(one, rows);
}

์ ์ฌ์ง์ print1 ํจ์๋ฅผ ์คํํด์ ์ป์ ๊ฒฐ๊ณผ๋ฅผ ๋ณด์ฌ์ฃผ๊ณ ์์ต๋๋ค.
์ด ํ๋ก๊ทธ๋จ์์ i๊ฐ 1์ฉ ์ฆ๊ฐํด๋ ๋ฉ๋ชจ๋ฆฌ ์ฃผ์๋ 4์ฉ ์ฆ๊ฐํ๋ ๊ฒ์ผ๋ก ๋ณด์ด๋ ์ด์ ๋, ๋ฐฐ์ด์ ๊ฐ ์์๊ฐ 4๋ฐ์ดํธ ํฌ๊ธฐ์ int์ด๊ธฐ ๋๋ฌธ์ ๋๋ค.
2.2 Dynamically Allocated Array ๋์ ์ผ๋ก ํ ๋น๋ ๋ฐฐ์ด
ํ๋ก๊ทธ๋จ์์ 100์ ํฌ๊ธฐ๋ฅผ ๊ฐ์ง๋ ๋ฐฐ์ด์ ์ ์ธํ๋ค๊ณ ํด๋ด ์๋ค.
int box[100];
๋ง์ผ ์ด ๋ฐฐ์ด์ 100๊ฐ๋ณด๋ค ๋ง์ ์๋ฅผ ๋ด์ผ๋ ค๊ณ ํ๋ค๋ฉด, ์ด๋ป๊ฒ ํด์ผ ํ ๊น์?
์ด์ ๋ํ ์ข์ ํด๋ต์, ์ด ๊ฒฐ์ ์ ์คํ ์๊ฐ๊น์ง ๋ฏธ๋ฃจ์๋ค๊ฐ ํ์ํ ๋ฐฐ์ด ํฌ๊ธฐ์ ์ ๋นํ ์ถ์ ์น๊ฐ ๋์ฌ ๋ ๋ฐฐ์ด์ ํ ๋นํ๋ ๊ฒ์ ๋๋ค.
int i, n, *list;
printf("Enter the number of number to generate: ");
scanf("%d, &n");
if(n < 1 )
{
fprintf(stderr, "Improper value of n\n");
exit(EXIT_FAILURE);
}
MALLOC(list, n * sizeof(int));
(1) 2์ฐจ์ ๋ฐฐ์ด
C ์ธ์ด๋ ๋ค์ฐจ์ ๋ฐฐ์ด์ ํํํ๊ธฐ ์ํ์ฌ ์์ ๋ฐฐ์ด์ ๋ฐฐ์ด์ ์ฌ์ฉํฉ๋๋ค.
int x[3][5];
๋ผ๊ณ ํ๋ฉด, ์ค์ ๋ก ๊ธธ์ด๊ฐ 3์ธ 1์ฐจ์ ๋ฐฐ์ด x๊ฐ ์์ฑ๋๋๋ฐ, ์ด x์ ๊ฐ ์์๋ ๊ธธ์ด๊ฐ 5์ธ 1์ฐจ์ ๋ฐฐ์ด์ ๋๋ค.
์๋ ๊ทธ๋ฆผ์ ํตํด ์ด์ ๋ํ ๋ฉ๋ชจ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ์์ธํ ๋ณผ ์ ์์ต๋๋ค.

์์ x[i][j]๋ฅผ ์ฐพ์ ๋ ์ ์ผ ๋จผ์ x[i]์ ์๋ ํฌ์ธํฐ์ ์ ๊ทผํฉ๋๋ค. ์ด ํฌ์ธํฐ๋ ๋ฐฐ์ด์ ํ i์ 0๋ฒ์งธ ์์์ ๋ฉ๋ชจ๋ฆฌ ์ฃผ์๋ฅผ ์ ๊ณตํ๊ณ , ๊ทธ๋ฌ๋ฉด ์ด ํฌ์ธํฐ์ j * sizeof(int)๋ฅผ ๋ํ๋ฉด ํ i์ [j]๋ฒ์งธ ์์(์ฆ, ์์ x[i][j])์ ์ฃผ์๊ฐ ๊ฒฐ์ ๋ฉ๋๋ค.
2.3 Structures and Unions ๊ตฌ์กฐ์ ์ ๋์ธ
๋ฐฐ์ด์ ๊ฐ์ ํ์ ์ ๋ฐ์ดํฐ ๋ชจ์์ ๋๋ค.
๊ทธ๋ ๋ค๋ฉด ๋ค๋ฅธ ํ์
์ ๋ฐ์ดํฐ๋ฅผ ๊ทธ๋ฃนํํ๋ ๋ฐฉ๋ฒ์ด ์์๊น์?
์ด๋ฅผ ๊ตฌ์กฐ์ฒด structure ๋ผ๊ณ ํฉ๋๋ค.
typedef struct {
char name[50];
int age;
float gpa;
} Student;
typedef struct {
int x, y;
} Point;
typedef struct {
int day;
int month;
int year;
} Date;
์์ ๊ฐ์ด typedef ๋ช ๋ น๋ฌธ์ ์ฌ์ฉํ์ฌ ๊ตฌ์กฐ ๋ฐ์ดํฐ ํ์ ์ ์์ฑํ ์ ์์ต๋๋ค.
typedef๋ฅผ ์ฌ์ฉํ๋ฉด ๋งค๋ฒ struct ํค์๋๋ฅผ ์ฐ์ง ์์๋ ๋๋ฏ๋ก ์ฝ๋๊ฐ ๋ ๊ฐ๊ฒฐํ๊ฒ ์์ฑ๋ฉ๋๋ค.
๊ตฌ์กฐ์ฒด๋ค์ ๋ฐ์ดํฐ ํ์ ์ด ๊ฐ์ ๋ person1 = person2์ ๊ฐ์ ๋ฐฉ์์ผ๋ก ๋ณต์ฌ๊ฐ ๊ฐ๋ฅํ์ง๋ง,
๋น๊ต ์ฐ์ฐ์๋ ๋ถ๊ฐ๋ฅํฉ๋๋ค. (Ex. if (person1 == person2) )
๋ฐ๋ผ์ ๊ตฌ์กฐ์ฒด ๊ฐ์ ๋น๊ต๋ฅผ ์ํด์๋ ์๋์ ๋ฐฉ์๊ณผ ๊ฐ์ด ์ผ์ผ์ด ๋น๊ต๋ฅผ ํด์ผ ํฉ๋๋ค.
#define FALSE 0
#define TRUE 1
int humansEqual (humanBeing person1, humanBeing person2)
{
if(strcmp(person1.name, person2.name))
return FALSE;
if(person1.age != person2.age)
return FALSE;
if(person1.salary != person2.salary)
return FALSE;
return TRUE;
}
๋ํ ๊ตฌ์กฐ์ฒด ์์ ๋ ๋ค๋ฅธ ๊ตฌ์กฐ์ฒด๋ฅผ ์ ์ํ ์๋ ์์ต๋๋ค. ์๋ฅผ ๋ค์ด humanBeing ๊ตฌ์กฐ์ฒด ์์ ์๋ ์์ผ์ ํฌํจํ๊ณ ์ ํ๋ ๊ฒฝ์ฐ ๋ค์๊ณผ ๊ฐ์ด ์์ฑํ ์ ์์ต๋๋ค.
typedef struct {
int month;
int day;
int year;
} date;
typedef struct {
char name[10];
int age;
float salary;
date dob;
} humanBeing;
humanBeing person1;
person1.dob.month = 12;
person1.dob.day = 8;
person1.dob.year = 1993;
(1) Unions ๊ณต์ฉ์ฒด
๊ณต์ฉ์ฒด์ ํน์ง์ ๊ณต์ฉ์ฒด ๋ด๋ถ์ ํ๋๋ค์ด ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ์ ๊ณต์ฉํ๋ค๋ ๊ฒ์ ๋๋ค.
์๋ฅผ ๋ค์ด, ์๋ ๊ณต์ฉ์ฒด์ ํฌ๊ธฐ๋ ์ผ๋ง์ธ์ง ๋ด ์๋ค.
union Data
{
int i;
float f;
char str[20];
};
์์ ๊ณต์ฉ์ฒด์ ํฌ๊ธฐ๋ 20๋ฐ์ดํธ์ ๋๋ค. (๊ฐ์ฅ ํฐ char str[20]์ ๋ฐ๋ผ..)
๋ฐ๋ฉด, ๊ตฌ์กฐ์ฒด์ ํฌ๊ธฐ๋ ํญ์ 4์ ๋ฐฐ์์ ๋๋ค.
๋ํ, ๊ตฌ์กฐ์ฒด ๋ด ํ๋์ ์์์ ๋ฐ๋ผ ๊ตฌ์กฐ์ฒด์ ํฌ๊ธฐ๊ฐ ๋ฌ๋ผ์ง ์ ์์ต๋ค.
#include <stdio.h>
typedef struct {
char a;
int b;
char c;
} InefficientStruct;
typedef struct {
int b;
char a;
char c;
} EfficientStruct;
int main()
{
printf("%lu bytes\n", sizeof(InefficientStruct));
printf("%lu bytes\n", sizeof(EfficientStruct));
return 0;
}
์ด ๊ฒฝ์ฐ, InefficientStruct๋ 12๋ฐ์ดํธ, EfficientStruct๋ 8๋ฐ์ดํธ์ ๋๋ค.
์ด ๋ ๊ตฌ์กฐ์ฒด์ ํฌ๊ธฐ๊ฐ ๋ค๋ฅธ ์ด์ ๋ ๋ฉ๋ชจ๋ฆฌ ํจ๋ฉ(Memory Padding) ๋๋ฌธ์ ๋๋ค. ๋ฉ๋ชจ๋ฆฌ ํจ๋ฉ์ ์ปดํ์ผ๋ฌ๊ฐ ์ฑ๋ฅ์ ์ต์ ํํ๊ธฐ ์ํด ๊ตฌ์กฐ์ฒด์ ๋ฉค๋ฒ๋ค์ ์ ๋ ฌ(Alignment)ํ๋ ๊ณผ์ ์์ ๋ฐ์ํฉ๋๋ค. (์๋ฅผ ๋ค์ด int ํ์ ์ 4๋ฐ์ดํธ ๊ฒฝ๊ณ์์ ์ ๋ ฌ๋๋๋ก ์ค์ ๋จ)
๊ฒฐ๋ก ์ ์ผ๋ก, ๊ตฌ์กฐ์ฒด์ ๋ฉค๋ฒ ์์์ ๋ฐ๋ผ ํจ๋ฉ์ด ๋ฌ๋ผ์ง๋ฉฐ, ์ด๋ ๊ตฌ์กฐ์ฒด์ ์ ์ฒด ํฌ๊ธฐ์ ์ํฅ์ ๋ฏธ์นฉ๋๋ค. ๋ฐ๋ผ์, ํจ๋ฉ์ ์ต์ํํ๋ ค๋ฉด, ๊ตฌ์กฐ์ฒด์ ๋ฉค๋ฒ๋ฅผ ํฐ ๋ฐ์ดํฐ ํ์ ๋ถํฐ ์์ ๋ฐ์ดํฐ ํ์ ์์๋ก ๋ฐฐ์นํ๋ ๊ฒ์ด ์ข์ต๋๋ค.
'๐CS > ๐Data Structure' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[์๋ฃ๊ตฌ์กฐ] hw1 ์ ๊ทผ ํ๊ธฐ๋ฒ ์ฆ๋ช (0) | 2024.10.15 |
---|---|
[์๋ฃ๊ตฌ์กฐ] Chapter3. STACKS AND QUEUES(2) (0) | 2024.10.15 |
[์๋ฃ๊ตฌ์กฐ] Chapter3. STACKS AND QUEUES(1) (2) | 2024.10.05 |
[์๋ฃ๊ตฌ์กฐ] Chapter2. Arrays and Structures(2) (0) | 2024.10.05 |