๊ตฌ์กฐ์ฒด(Structure)๋ C ์ธ์ด์์ ์ฌ๋ฌ ๊ฐ์ ๋ค๋ฅธ ๋ฐ์ดํฐ ํ์ ์ ๋ฌถ์ด์ ํ๋์ ์๋ก์ด ๋ฐ์ดํฐ ํ์ ์ ๋ง๋๋ ๋ฐ ์ฌ์ฉ๋๋ ๊ธฐ๋ฅ์ ๋๋ค.
๊ตฌ์กฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ฉด ์๋ก ๋ค๋ฅธ ํ์ ์ ๋ณ์๋ค์ ํ๋๋ก ๋ฌถ์ด์ ์๋ก์ด ์๋ฃํ์ ์ ์ํ ์ ์์ต๋๋ค. ์ด๋ ๋ณต์กํ ๋ฐ์ดํฐ๋ฅผ ํจ๊ณผ์ ์ผ๋ก ํํํ๊ณ ๋ค๋ฃจ๋ ๋ฐ ๋์์ด ๋ฉ๋๋ค.
#include <stdio.h>
#include <string.h>
// ํ์ ์ ๋ณด๋ฅผ ๋ํ๋ด๋ ๊ตฌ์กฐ์ฒด ์ ์
struct Student {
char name[50];
int age;
float gpa;
};
int main() {
// ๊ตฌ์กฐ์ฒด ๋ณ์ ์ ์ธ๊ณผ ์ด๊ธฐํ
struct Student student1;
strcpy(student1.name, "John Doe");
student1.age = 20;
student1.gpa = 3.5;
// ๋ ๋ค๋ฅธ ๊ตฌ์กฐ์ฒด ๋ณ์ ์ ์ธ๊ณผ ์ด๊ธฐํ
struct Student student2;
strcpy(student2.name, "Jane Smith");
student2.age = 22;
student2.gpa = 3.9;
// ๋ฐฐ์ด์ ์ฌ์ฉํ์ฌ ์ฌ๋ฌ ํ์ ์ ๋ณด ๊ด๋ฆฌ
struct Student students[2] = {student1, student2};
// ํ์ ์ ๋ณด ์ถ๋ ฅ
for (int i = 0; i < 2; ++i) {
printf("ํ์ %d\n", i + 1);
printf("์ด๋ฆ: %s\n", students[i].name);
printf("๋์ด: %d\n", students[i].age);
printf("ํ๊ท ์ฑ์ : %.2f\n", students[i].gpa);
printf("\n");
}
return 0;
}
์ด์ student1๊ณผ student2๋ฅผ ๋ฐฐ์ด์ ๋ฃ์ด์ ์ฌ๋ฌ ํ์ ์ ๋ณด๋ฅผ ๊ด๋ฆฌํ๊ณ , ๋ฐ๋ณต๋ฌธ์ ์ฌ์ฉํ์ฌ ๊ฐ ํ์์ ์ ๋ณด๋ฅผ ์ถ๋ ฅํ ์ ์์ต๋๋ค. ์ด๋ฌํ ๋ฐฉ์์ผ๋ก ๊ตฌ์กฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฐ์ดํฐ๋ฅผ ๊ตฌ์กฐ์ ์ผ๋ก ๊ด๋ฆฌํ๋ฉด์ ์ฝ๋๋ฅผ ๊ฐ๊ฒฐํ๊ณ ํ์ฅ ๊ฐ๋ฅํ๊ฒ ์์ฑํ ์ ์์ต๋๋ค.
Java์ ํด๋์ค(class) vs C์ธ์ด์ ๊ตฌ์กฐ์ฒด(structure)
Java์ ํด๋์ค(class)์ C ์ธ์ด์ ๊ตฌ์กฐ์ฒด(structure)๋ ๋ชจ๋ ๋ฐ์ดํฐ๋ฅผ ๋ฌถ์ด์ ๊ด๋ฆฌํ๋ ๋ฐ ์ฌ์ฉ๋๋ ๊ฐ๋ ์ด์ง๋ง, ๋ช ๊ฐ์ง ์ค์ํ ์ฐจ์ด์ ์ด ์์ต๋๋ค.
Java์ ํด๋์ค (Class)
- ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ(OOP): Java๋ ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์ด๋ฉฐ, ํด๋์ค๋ ์ด ๊ฐ๋ ์ ๊ธฐ๋ฐ์ผ๋ก ํฉ๋๋ค. ํด๋์ค๋ ๋ฐ์ดํฐ(๋ฉค๋ฒ ๋ณ์)์ ํด๋น ๋ฐ์ดํฐ๋ฅผ ๋ค๋ฃจ๋ ๋ฉ์๋(๋ฉค๋ฒ ํจ์)๋ฅผ ํจ๊ป ๋ฌถ์ด๋์ ๊ฐ๋ ์ ๋๋ค.
- ์์, ๋คํ์ฑ, ์บก์ํ: Java ํด๋์ค๋ ์์, ๋คํ์ฑ, ์บก์ํ ๋ฑ ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ์ ํน์ง์ ์ง์ํฉ๋๋ค. ์ด๋ ์ฝ๋์ ์ฌ์ฌ์ฉ์ฑ์ ๋์ด๊ณ , ์ ์ง๋ณด์๋ฅผ ์ฉ์ดํ๊ฒ ํฉ๋๋ค.
- ํ ๋ฉ๋ชจ๋ฆฌ ํ ๋น: ๊ฐ์ฒด๋ ํ ๋ฉ๋ชจ๋ฆฌ์ ๋์ ์ผ๋ก ํ ๋น๋๋ฉฐ, ์ฐธ์กฐ ๋ณ์๋ฅผ ํตํด ์ ๊ทผ๋ฉ๋๋ค.
- ์์ฑ์์ ์๋ฉธ์: ํด๋์ค๋ ์์ฑ์(constructor)์ ์๋ฉธ์(destructor)๋ฅผ ๊ฐ์ง ์ ์์ต๋๋ค. ์์ฑ์๋ ๊ฐ์ฒด๊ฐ ์์ฑ๋ ๋ ํธ์ถ๋๋ฉฐ, ์๋ฉธ์๋ ๊ฐ์ฒด๊ฐ ์๋ฉธ๋ ๋ ํธ์ถ๋ฉ๋๋ค.
- ์ธํฐํ์ด์ค์ ์ถ์ ํด๋์ค: Java์์๋ ์ธํฐํ์ด์ค(interface)์ ์ถ์ ํด๋์ค(abstract class)๋ฅผ ํตํด ์ถ์ํ๋ฅผ ์ง์ํฉ๋๋ค.
C ์ธ์ด์ ๊ตฌ์กฐ์ฒด (Structure)
- ์ ์ฐจ ์งํฅ ํ๋ก๊ทธ๋๋ฐ: C ์ธ์ด๋ ์ฃผ๋ก ์ ์ฐจ ์งํฅ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด์ด๋ฉฐ, ๊ตฌ์กฐ์ฒด๋ ๋ฐ์ดํฐ๋ฅผ ๋ฌถ์ด ๊ด๋ฆฌํ๋ ๋ฐ ์ฃผ๋ก ์ฌ์ฉ๋ฉ๋๋ค. ํจ์์ ๋ฐ์ดํฐ๊ฐ ๋ถ๋ฆฌ๋์ด ์์ต๋๋ค.
- ํฌ์ธํฐ ํ์ฉ: ๊ตฌ์กฐ์ฒด๋ ์ฃผ๋ก ํฌ์ธํฐ๋ฅผ ์ด์ฉํ์ฌ ๋ค๋ฃจ์ด์ง๋๋ค. ํฌ์ธํฐ๋ฅผ ํตํด ๊ตฌ์กฐ์ฒด์ ๋ฉค๋ฒ์ ์ ๊ทผํ๊ฑฐ๋ ๋์ ๋ฉ๋ชจ๋ฆฌ ํ ๋น์ด ์ด๋ฃจ์ด์ง๋๋ค.
- ์คํ ๋ฉ๋ชจ๋ฆฌ ํ ๋น: ๊ตฌ์กฐ์ฒด ๋ณ์๋ ์ฃผ๋ก ์คํ ๋ฉ๋ชจ๋ฆฌ์ ํ ๋น๋๋ฉฐ, ๊ฐ์ ์ง์ ์ ๊ทผ๋ฉ๋๋ค.
- ์์ฑ์์ ์๋ฉธ์ ๋ถ์ฌ: C ์ธ์ด์๋ ๋ช ์์ ์ธ ์์ฑ์์ ์๋ฉธ์๊ฐ ์์ต๋๋ค. ๊ฐ๋ฐ์๊ฐ ์ง์ ์ด๊ธฐํ์ ํด์ ๋ฅผ ๊ด๋ฆฌํด์ผ ํฉ๋๋ค.
Keyword typedef
typedef๋ C ์ธ์ด์์ ์ฌ์ฉ๋๋ ํค์๋๋ก, ์๋ก์ด ๋ฐ์ดํฐ ํ์ ์ ๋ณ์นญ(alias)์ ์ง์ ํ ๋ ์ฌ์ฉ๋ฉ๋๋ค. typedef๋ฅผ ์ฌ์ฉํ๋ฉด ๊ธฐ์กด์ ๋ฐ์ดํฐ ํ์ ์ ์๋ก์ด ์ด๋ฆ์ ๋ถ์ฌํ์ฌ ์ฝ๋๋ฅผ ๋ ์ฝ๊ธฐ ์ฝ๊ฒ ๋ง๋ค๊ฑฐ๋, ์ฝ๋์ ์ด์์ฑ์ ํฅ์์ํค๋ ๋ฐ ๋์์ด ๋ฉ๋๋ค.
ํฌ์ธํฐ ๊ตฌ์กฐ์ฒด์ ์ ๊ทผ
ํฌ์ธํฐ๋ฅผ ์ด์ฉํ์ฌ ๊ตฌ์กฐ์ฒด ๋ฉค๋ฒ์ ์ ๊ทผํ ๋๋ -> ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํด์ผ ํฉ๋๋ค. -> ์ฐ์ฐ์๋ ํฌ์ธํฐ๊ฐ ๊ฐ๋ฆฌํค๋ ๊ตฌ์กฐ์ฒด์ ๋ฉค๋ฒ์ ์ ๊ทผํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค. ๊ตฌ์กฐ์ฒด ํฌ์ธํฐ๋ฅผ ์ฌ์ฉํ ๋ -> ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ์ง ์์ผ๋ฉด ์ปดํ์ผ ์ค๋ฅ๊ฐ ๋ฐ์ํฉ๋๋ค.
#include <stdio.h>
// ํ์ ์ ๋ณด๋ฅผ ๋ํ๋ด๋ ๊ตฌ์กฐ์ฒด ์ ์
struct Student {
char name[50];
int age;
float gpa;
};
int main() {
// ๊ตฌ์กฐ์ฒด ํฌ์ธํฐ ์ ์ธ๊ณผ ๋ฉ๋ชจ๋ฆฌ ํ ๋น
struct Student* studentPtr = malloc(sizeof(struct Student));
// ํฌ์ธํฐ๋ฅผ ์ฌ์ฉํ์ฌ ๊ตฌ์กฐ์ฒด ๋ฉค๋ฒ์ ๊ฐ ํ ๋น
strcpy(studentPtr->name, "John Doe");
studentPtr->age = 20;
studentPtr->gpa = 3.5;
// ํฌ์ธํฐ๋ฅผ ์ฌ์ฉํ์ฌ ๊ตฌ์กฐ์ฒด ๋ฉค๋ฒ์ ๊ฐ ์ถ๋ ฅ
printf("์ด๋ฆ: %s\n", studentPtr->name);
printf("๋์ด: %d\n", studentPtr->age);
printf("ํ๊ท ์ฑ์ : %.2f\n", studentPtr->gpa);
// ๋ฉ๋ชจ๋ฆฌ ํด์
free(studentPtr);
return 0;
}
์ ์์ ์์ -> ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ์ฌ studentPtr์ด ๊ฐ๋ฆฌํค๋ ๊ตฌ์กฐ์ฒด์ ๋ฉค๋ฒ์ ์ ๊ทผํฉ๋๋ค. ๊ตฌ์กฐ์ฒด ํฌ์ธํฐ๋ฅผ ์ฌ์ฉํ ๋ -> ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ๋ ๊ฒ์ด ์ผ๋ฐ์ ์ด๋ฉฐ, ์ด๋ฅผ ํตํด ํฌ์ธํฐ๊ฐ ๊ฐ๋ฆฌํค๋ ๊ตฌ์กฐ์ฒด์ ๋ฉค๋ฒ์ ์ ๊ทผํ ์ ์์ต๋๋ค.
๊ตฌ์กฐ์ฒด ์์ ๋ค๋ฅธ ๊ตฌ์กฐ์ฒด
๊ตฌ์กฐ์ฒด ์์ ๋ค๋ฅธ ๊ตฌ์กฐ์ฒด๋ฅผ ํฌํจํ๋ ๊ฒ์ด ๊ฐ๋ฅํฉ๋๋ค. ์ด๋ฅผ "์ค์ฒฉ ๊ตฌ์กฐ์ฒด" ๋๋ "๊ตฌ์กฐ์ฒด ๋ด๋ถ์ ๊ตฌ์กฐ์ฒด"๋ผ๊ณ ๋ถ๋ฆ ๋๋ค. ์ค์ฒฉ ๊ตฌ์กฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ฉด ์ฌ๋ฌ ๊ฐ์ง ๋ฐ์ดํฐ๋ฅผ ๊ณ์ธต์ ์ผ๋ก ๊ตฌ์กฐํํ ์ ์์ต๋๋ค.
#include <stdio.h>
// ์ฃผ์ ์ ๋ณด๋ฅผ ๋ํ๋ด๋ ๊ตฌ์กฐ์ฒด
struct Address {
char street[50];
char city[30];
int zipCode;
};
// ํ์ ์ ๋ณด๋ฅผ ๋ํ๋ด๋ ๊ตฌ์กฐ์ฒด
struct Student {
char name[50];
int age;
float gpa;
struct Address address; // ์ค์ฒฉ๋ Address ๊ตฌ์กฐ์ฒด
};
int main() {
// ์ค์ฒฉ๋ ๊ตฌ์กฐ์ฒด ์ฌ์ฉ ์์
struct Student student1;
// ์ฃผ์ ์ ๋ณด ์
๋ ฅ
strcpy(student1.address.street, "123 Main Street");
strcpy(student1.address.city, "Cityville");
student1.address.zipCode = 12345;
// ํ์ ์ ๋ณด ์
๋ ฅ
strcpy(student1.name, "John Doe");
student1.age = 20;
student1.gpa = 3.5;
// ์ ๋ณด ์ถ๋ ฅ
printf("์ด๋ฆ: %s\n", student1.name);
printf("๋์ด: %d\n", student1.age);
printf("ํ๊ท ์ฑ์ : %.2f\n", student1.gpa);
printf("์ฃผ์: %s, %s, %d\n", student1.address.street, student1.address.city, student1.address.zipCode);
return 0;
}
์ ์์ ์์ struct Student ์์ struct Address๊ฐ ์ค์ฒฉ๋์ด ์์ต๋๋ค. ์ด๋ ๊ฒ ์ค์ฒฉ๋ ๊ตฌ์กฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ฉด ์ฝ๋๋ฅผ ์กฐ์งํํ๊ณ , ๊ด๋ จ ์๋ ์ ๋ณด๋ค์ ๋ ํจ๊ณผ์ ์ผ๋ก ํํํ ์ ์์ต๋๋ค.
'Programming > C language' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
15 File input & output (1) | 2024.01.25 |
---|---|
13 Array (0) | 2024.01.24 |
12 Pointer (1) | 2024.01.24 |
11 function (ํจ์) (0) | 2024.01.23 |
10 string (feat. random ํจ์) (0) | 2024.01.23 |
๋๊ธ