1๏ธโฃ ๊ตฌ์กฐ์ฒด ์ ์ธ๊ณผ ์ฌ์ฉ โจ
๊ตฌ์กฐ์ฒด๋ ํ๋ ์ด์์ ์๋ก ๋ค๋ฅธ ์ข ๋ฅ์ ๋ณ์๋ค์ ๋ฌถ์ด์ ์๋ก์ด ์๋ฃํ์ ์ ์ํ๋ ๊ฒ์ด๋ค.
์ด๋ฅผ ํตํด ์ฐ๊ด๋ ๋ฐ์ดํฐ๋ฅผ ํ๋๋ก ๋ฌถ์ด์ ๊ด๋ฆฌ ํ ์ ์๋ค. ์๋ฅผ ๋ค์ด ํ์ ์ ๋ณด๋ฅผ ํํํ๋ ๊ตฌ์กฐ์ฒด๋ ์ด๋ฆ, ๋์ด, ํค ๋ฑ์ ๋ณ์๋ค๋ก ์ด๋ฃจ์ด์ง์ ์๋ค.
๊ตฌ์กฐ์ฒด ์ฌ์ฉํ๊ธฐ
๊ตฌ์กฐ์ฒด๋ ์ฌ์ฉ์๊ฐ ์ ์ํ ์๋ก์ด ์๋ฃํ์ผ๋ก, ๊ตฌ์กฐ์ฒด ๋ณ์๋ฅผ ์ ์ธํ์ฌ ์ฌ์ฉํ๋ค.
๊ตฌ์กฐ์ฒด ๋ณ์๋ฅผ ์ ์ธํ ๋๋ ์ง์ ์ ๊ทผํ์ฌ ๊ฐ์ ํ ๋นํ๊ฑฐ๋ ์ฝ์ ์ ์๋ค. ๋ํ, ํฌ์ธํฐ๋ฅผ ์ฌ์ฉํ์ฌ ๊ตฌ์กฐ์ฒด์ ๋์ ์ผ๋ก ๋ฉ๋ชจ๋ฆฌ๋ฅผ ํ ๋นํ ์๋ ์๋ค.
โน ์ผ๋ฐ์ ์ธ ๋ฐฉ๋ฒ
struct student
{
char name[10];
int age;
int height;
};
int main(void)
{
struct student st1;
struct student st2;
}
โน ๊ตฌ์กฐ์ฒด ๋ณ์๋ฅผ ์ ์ธ๊ณผ ๋์์ ์ด๊ธฐํํ๋ ๋ฐฉ๋ฒ
struct student
{
char name[10];
int age;
int height;
} st1, st2;
์ ์ธ๋ ๊ตฌ์กฐ์ฒด ๋ณ์์ ํํ๋ ์์ ๊ทธ๋ฆผ๊ณผ ๊ฐ์ด ๊ฐ๊ฐ 18Byte์ ๊ณต๊ฐ์ ํ๋ณดํ๊ณ ์์ผ๋ฉฐ, name, age, height 3๊ฐ์ง ๋ฉค๋ฒ๋ฅผ ํตํด ์ ๊ทผํ ์ ์๋ค.
2๏ธโฃ ๊ตฌ์กฐ์ฒด ๋ฉค๋ฒ ์ ๊ทผโจ
๊ตฌ์กฐ์ฒด ๋ฉค๋ฒ์ ์ ๊ทผํ๋ ๋ฐฉ๋ฒ์ ๊ตฌ์กฐ์ฒด ๋ณ์๋ฅผ ์ฌ์ฉํ์ฌ ๋ฉค๋ฒ์ ๊ฐ์ ์ฐธ์กฐํ๋ ๊ฒ์ด๋ค. ์ฃผ๋ก ์ง์ ์ ๊ทผ(.) ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ๊ฑฐ๋ ํฌ์ธํฐ๋ฅผ ์ด์ฉํ์ฌ ๊ฐ์ ์ (->)์ผ๋ก ์ ๊ทผํ ์ ์๋ค.
๊ตฌ์กฐ์ฒด ๋ฉค๋ฒ์ ์ง์ ์ ๊ทผ
st1.age = 23;
st1.height = 167;
strcpy(st1.name, "Kim");
์์ ์ฝ๋๋ st1 ๊ตฌ์กฐ์ฒด ๋ณ์์ ๋ฉค๋ฒ์ธ age, height, name์ ๊ฐ๊ฐ ๊ฐ์ ํ ๋นํ๊ณ ์๋ค.
ํฌ์ธํฐ๋ฅผ ํตํ ๊ฐ์ ์ ๊ทผ
๊ตฌ์กฐ์ฒด ๋ณ์๋ฅผ ํฌ์ธํฐ๋ก ์ฌ์ฉํ ๋์๋ ํฌ์ธํฐ๊ฐ ๊ฐ๋ฆฌํค๋ ๊ตฌ์กฐ์ฒด์ ๋ฉค๋ฒ์ ์ ๊ทผํ๋ค. ์ด๋์๋ ํ์ดํ ๊ฐ์ ์ฐธ์กฐ(->) ์ฐ์ฐ์๋ฅผ ์ฌ์ฉํ๋ค.
struct student
{
char name[10];
int age;
int height;
}st, *pSt;
int main(void)
{
pSt = &st;
strcpy(pSt -> name, "ํจ๋");
pSt -> age = 23;
pSt -> height = 167;
printf("์ด๋ฆ: %s, ๋์ด: %d, ํค: %d\n", pSt->name, pSt->age, pSt->height);
return 0;
}
์ ์ฝ๋๋ pSt ํฌ์ธํฐ ๋ณ์๊ฐ st ๊ตฌ์กฐ์ฒด ๋ณ์๋ฅผ ๊ฐ๋ฆฌํค๊ณ , ํฌ์ธํฐ๋ฅผ ํตํด ๊ตฌ์กฐ์ฒด์ ๋ฉค๋ฒ์ ์ ๊ทผํ์ฌ ๊ฐ์ ๋ณ๊ฒฝํ๋ค.
๊ตฌ์กฐ์ฒด ๋ณ์๋ฅผ ํฌ์ธํฐ๋ก ์ฌ์ฉํ ๋๋ ํฌ์ธํฐ๊ฐ ์ ํจํ ๋ฉ๋ชจ๋ฆฌ ์ฃผ์๋ฅผ ๊ฐ๋ฆฌํค๊ณ ์์ด์ผ ํ๋ค. ๊ทธ๋ ์ง ์์ผ๋ฉด ์๋ชป๋ ๋ฉ๋ชจ๋ฆฌ ์ ๊ทผ์ผ๋ก ์ธํ ์ค๋ฅ๊ฐ ๋ฐ์ํ ์ ์๋ค.
3๏ธโฃ ๊ตฌ์กฐ์ฒด ๋ฐฐ์ด โจ
๊ตฌ์กฐ์ฒด๋ ์๋ฃํ์ผ๋ก์จ ๋ฐฐ์ดํ์ผ๋ก ์ ์ธํ ์ ์๋ค. ์ฌ๋ฌ ์ฌ๋์ ์ ๋ณด๋ฅผ ํจ๊ณผ์ ์ผ๋ก ๊ด๋ฆฌํ๊ธฐ ์ํด ๊ตฌ์กฐ์ฒด ๋ณ์๋ฅผ ๋ฐฐ์ด๋ก ์ ์ธํ ์ ์๋ค.
struct student
{
char name[10];
int age;
double height;
} st[5];
๋๋
student st[5]; // st์ด๋ฆ์ผ๋ก 5๊ฐ ๋ฐฐ์ด ์ ์ธ
๐ ๊ตฌ์กฐ์ฒด ๋ฐฐ์ด ์ฝ๋
#include <stdio.h>
#include <string.h>
struct student
{
char name[10];
int age;
double height;
} st[4] = {
{"ํจ๋", 23, 166.9},
{"์ง์ ", 22, 159.5},
{"์์ง", 23, 166},
{"์ง์ธ", 23, 167.3}
};
int main(void)
{
int i;
printf("๊ตฌ์กฐ์ฒด ๋ฉค๋ฒ์ ์ด๊ธฐ๊ฐ ์ถ๋ ฅ\n");
for(i = 0; i < sizeof(st)/sizeof(st[0]); i++)
{
printf("์ด๋ฆ: %s, ๋์ด: %d, ํค: %.2f\n", st[i].name, st[i].age, st[i].height);
}
st[1].height = 161;
printf("\n๋ฐ์ดํฐ ๋ณ๊ฒฝ ํ\n");
printf("์ด๋ฆ: %s, ๋์ด: %d, ํค: %.2f\n", st[1].name, st[1].age, st[1].height);
return 0;
}
์ ์ฝ๋๋ ๊ฐ ๊ตฌ์กฐ์ฒด์ ๋ฉค๋ฒ์ ์ด๊ธฐ๊ฐ์ ์ค์ ํ ํ, ๋ฐฐ์ด์ ์ํํ๋ฉด์ ์ด๊ธฐ๊ฐ์ ์ถ๋ ฅํ๋ค. ๊ทธ๋ฆฌ๊ณ ๋ฐฐ์ด์ ํ ์์์ ๋ฐ์ดํฐ๋ฅผ ๋ณ๊ฒฝํ ํ ๋ณ๊ฒฝ๋ ๋ด์ฉ์ ์ถ๋ ฅํ๋ค.
4๏ธโฃ ๊ตฌ์กฐ์ฒด ํฌ์ธํฐ: ๋ฉ๋ชจ๋ฆฌ ์ฃผ์๋ฅผ ํตํ ๋ฐ์ดํฐ ์ ๊ทผ โจ
๊ตฌ์กฐ์ฒด๋ ์๋ฃํ์ด๋ฏ๋ก ํฌ์ธํฐํ์ผ๋ก ์ ์ธ ํ ์ ์๋ค. ์ด๋ฅผ ํตํด ๊ตฌ์กฐ์ฒด ๋ณ์์ ์ฃผ์๋ฅผ ์ ์ฅํ๊ณ , ํด๋น ์ฃผ์๋ฅผ ํตํด ๊ตฌ์กฐ์ฒด ๋ฉค๋ฒ์ ์ ๊ทผํ ์์๋ค. ํฌ์ธํฐ๋ฅผ ํตํด ๊ตฌ์กฐ์ฒด ๋ฉค๋ฒ์ ์ ๊ทผํ ๋๋ ๊ฐ์ ์ ๊ทผ ์ฐ์ฐ์ (->)๋ฅผ ์ฌ์ฉํ๋ค.
๊ตฌ์กฐ์ฒด ํฌ์ธํฐ ์ ์ธ
๊ตฌ์ด์ฒด๋ฅผ ํฌ์ธํฐ๋ก ์ ์ธํ ๋๋ ํฌ์ธํฐ ๋ณ์์ ํํ๋ฅผ ๋ฐ๋ฅด๊ณ , ๊ตฌ์กฐ์ฒด ๋ณ์์ ์ฃผ์๋ฅผ ๊ฐ๋ฆฌํค๊ฒ๋๋ค.
struct student *pSt; // *pSt 4Byte ์ฐจ์ง
๊ตฌ์กฐ์ฒด ํฌ์ธํฐ ์ฝ๋
#include <stdio.h>
#include <string.h>
struct student
{
char name[10];
int age;
double height;
} st[4] = {
{"ํจ๋", 23, 166.9},
{"์ง์ ", 22, 159.5},
{"์์ง", 23, 166},
{"์ง์ธ", 23, 167.3}
};
int main(void)
{
int i;
struct student *pSt;
pSt = st;
printf("๊ตฌ์กฐ์ฒด ๋ฐฐ์ด ๋ฉค๋ฒ์ ์ด๊ธฐ๊ฐ ์ถ๋ ฅ\n");
for(i = 0; i < sizeof(st)/sizeof(st[0]); i++)
{
printf("์ด๋ฆ: %s, ๋์ด: %d, ํค: %.2f\n", st[i].name, st[i].age, st[i].height);
}
printf("\n๊ตฌ์กฐ์ฒด ํฌ์ธํฐ๋ฅผ ์ด์ฉํ ์ถ๋ ฅ\n");
for(i = 0; i < sizeof(st)/sizeof(st[0]); i++)
{
printf("์ด๋ฆ: %s, ๋์ด: %d, ํค: %.2f\n", (pSt + i)->name, (pSt + i)->age, (pSt + i)->height);
}
return 0;
}
์ ์ฝ๋๋ ๊ตฌ์กฐ์ฒด ํฌ์ธํฐ๋ฅผ ์ด์ฉํ์ฌ ๊ตฌ์กฐ์ฒด ๋ฐฐ์ด์ ๊ฐ ์์์ ์ ๊ทผํ๋ ๋ฐฉ๋ฒ์ด๋ค. ํฌ์ธํฐ ๋ณ์ pSt๊ฐ ๊ตฌ์กฐ์ฒด ๋ฐฐ์ด st์ ์ฒซ๋ฒ์งธ ์์๋ฅผ ๊ฐ๋ฆฌํค๊ณ , ์ด๋ฅผ ํตํด ๊ฐ ์์์ ๋ฉค๋ฒ์ ์ ๊ทผํ๋ค.
5๏ธโฃ ๊ตฌ์กฐ์ฒด ์ด๊ธฐํ
์ผ๋ฐ์ ์ด๊ธฐํ ๋ฐฉ๋ฒ
๊ตฌ์กฐ์ฒด ๋ณ์์ ๊ฐ ๋ฉค๋ฒ์ ๊ฐ์ ํ ๋นํ๋ ๋ฐฉ์์ผ๋ก ์ด๊ธฐํํ๋ค.
์ด ๋ฐฉ๋ฒ์ ๊ตฌ์กฐ์ฒด ๋ณ์๋ฅผ ๋จผ์ ์ ์ธ ํ ํ์ ๊ฐ์ ํ ๋นํ๋ ๋ฐฉ๋ฒ์ด๋ค.
struct student
{
char name[10];
int age;
int height;
} st;
st.age = 23;
st.height = 167;
strcpy(st.name, "ํจ๋");
์ ์ธ๊ณผ ๋์์ ์ด๊ธฐํํ๋ ๋ฐฉ๋ฒ
๊ตฌ์กฐ์ฒด ๋ณ์๋ ์ ์ธ๊ณผ ๋์์ ์ด๊ธฐํํ ์ ์๋ค. ์ค๊ดํธ {} ์์ ์ด๊ธฐ๊ฐ์ ์ง์ ํ์ฌ ํ๋ฒ์ ์กฐ๊ธฐํ ํ ์ ์๋ค.
struct student
{
char name[10];
int age;
int height;
} st = {"ํจ๋", 23, 167};
๋๋ ๊ตฌ์กฐ์ฒด ๋ณ์๋ฅผ ์ ์ธํ๋ ๋์์ ๋ณ์ ์ด๋ฆ์ ์๋ตํ๊ณ ์ด๊ธฐํํ ์๋ ์๋ค.
struct student
{
char name[10];
int age;
int height;
} = {"ํจ๋", 23, 167};