๊ตฌ์กฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ฉด ๋ณ์๋ค์ ํ๋๋ก ๋ฌถ์ ์ ์๋ค.
๊ตฌ์กฐ์ฒด๋ ๋ณ์๋ฅผ ํ๋๋ก ๋ฌถ์ ์๋ฃํ์ด ํ๋ ์ถ๊ฐ๋ ๊ฒ์ด์ง, ๋ณ์๋ ์๋๊ธฐ ๋๋ฌธ์ ๋ฉ๋ชจ๋ฆฌ์ ๊ณต๊ฐ์ด ์๊ธด ๊ฒ์ ์๋๋ค
๊ทธ๋ฌ๋ฏ๋ก ๊ตฌ์กฐ์ฒด๋ฅผ ๋ณ์๋ก ์ฌ์ฉํ๊ธฐ ์ํด์๋ ๋ณ์๋ก ์ ์ธํ์ฌ์ผ ํ๋ค.
struct student {
int number; // ํ๋ฒ
char name[10]; // ์ด๋ฆ
double grade; // ํ์
};
๊ตฌ์กฐ์ฒด๋ฅผ ๋ณ์๋ก ์ ์ธํ๊ธฐ ์ํด์๋?
int main(void){
struct student s1; //๊ตฌ์กฐ์ฒด ๋ณ์ ์ ์ธ
}
๊ตฌ์กฐ์ฒด์ ๋ฐฐ์ด์ ์ฐจ์ด์
๊ตฌ์กฐ์ฒด : ์๋ก ๋ค๋ฅธ ๋ฐ์ดํฐ๋ฅผ ํ๊บผ๋ฒ์ ๋ชจ์์ ์๋ฏธ๋จ์๋ก ๋ฌถ์ด์ ์ค๋ช ํ๋ ๊ฒ
๋ฐฐ์ด : ๋ฐฐ์ด์ ๋์ผํ ๋ฐ์ดํฐ ํ์ ์ ์ฌ๋ฌ ์์๋ฅผ ํ๋๋ก ๋ฌถ์ด ๊ด๋ฆฌํ๋ ์๋ฃ ๊ตฌ์กฐ
๊ตฌ์กฐ์ฒด์ ์ด๊ธฐํ
- ์ค๊ดํธ๋ฅผ ์ด์ฉํ์ฌ ์ด๊ธฐ๊ฐ์ ๋์ดํ๋ค
struct student {
int number;
char name[10];
double grade;
};
struct student s1 = { 24, "Kim", 4.3 }; // ์ด๊ธฐ๊ฐ
๊ตฌ์กฐ์ฒด ๋ฉค๋ฒ ์ฐธ์กฐ
s1.grade = 3.8;
struct student {
int number;
char name[10];
double grade;
};
int main(void){
struct student s;
s.number = 20190001;
strcpy(s.name, "ํ๊ธธ๋");
s.grade = 4.3
pritnf("ํ๋ฒ:%d\n", s.number);
pritnf("์ด๋ฆ:%s\n", s.name);
pritnf("ํ์ :%f\n", s.grade);
return 0;
};
์์
- ๋ ์ ์ฌ์ด์ ๊ฑฐ๋ฆฌ ๊ตฌํ๊ธฐ
#include <math.h>
struct point {
int x;
int y;
};
int main(void)
{
struct point p1, p2;
int xdiff, ydiff;
double dist;
printf("์ ์ ์ขํ๋ฅผ ์
๋ ฅํ์์ค(x y): ");
scanf("%d %d", &p1.x, &p1.y);
printf("์ ์ ์ขํ๋ฅผ ์
๋ ฅํ์์ค(x y): ");
scanf("%d %d", &p2.x, &p2.y);
xdiff = p1.x - p2.x;
ydiff = p1.y - p2.y;
dist = sqrt((double( xdiff * xdiff + ydiff * ydiff )));
printf("๋ ์ ์ฌ์ด์ ๊ฑฐ๋ฆฌ๋ %f์
๋๋ค.\n", dist);
return 0;
}
๊ตฌ์กฐ์ฒด๋ฅผ ๋ฉค๋ฒ๋ก ๊ฐ์ง๋ ๊ตฌ์กฐ์ฒด (๊ตฌ์กฐ์ฒด ์์ ๊ตฌ์กฐ์ฒด ํฌํจํ๊ธฐ)
struct date {
int year;
int month;
int day;
};
struct student { //๊ตฌ์กฐ์ฒด ์ ์ธ
int number;
char name[10];
struct date dob; //๊ตฌ์กฐ์ฒด ์์ ๊ตฌ์กฐ์ฒด ํฌํจ
double grade;
};
struct student s1 //๊ตฌ์กฐ์ฒด ๋ณ์ ์ ์ธ
s1.dob.year = 1983; //๋ฉค๋ฒ ์ฐธ์กฐ
s1.dob.month = 03;
s1.dob.day = 29;
๊ตฌ์กฐ์ฒด ๋ณ์์ ๋์ ๊ณผ ๋น๊ต
- ๊ฐ์ ๊ตฌ์กฐ์ฒด ๋ณ์๋ผ๋ฆฌ ๋์ ์ ๊ฐ๋ฅํ์ง๋ง ๋น๊ต๋ ๋ถ๊ฐ๋ฅํ๋ค
struct point{
int x;
int y;
}
int main(void)
{
struct point p1 = {10, 20};
struct point p2 = {30, 40};
p2 = p1; // ๋์
๊ฐ๋ฅ
if(p1 == p2) //์ปดํ์ผ ์ค๋ฅ!! ๋น๊ต ๋ถ๊ฐ
printf("p1๊ณผ p2์ด ๊ฐ์ต๋๋ค")
if((p1.x == p2.x) && (p1.y == p2.y)) //์ฌ๋ฐ๋ฅธ ๋น๊ต
printf("p1๊ณผ p2์ด ๊ฐ์ต๋๋ค")
}
๊ตฌ์กฐ์ฒด ๋ฐฐ์ด ์ ์ธ
- ๊ตฌ์กฐ์ฒด ๋ฐฐ์ด์ด๋ ๊ตฌ์กฐ์ฒด๋ฅผ ์ฌ๋ฌ ๊ฐ ๋ชจ์ ๊ฒ์ ๋งํ๋ค.
struct student {
int number;
char name[20];
double grade;
};
int main(void)
{
struct student list[100]; //๊ตฌ์กฐ์ฒด์ ๋ฐฐ์ด ์ ์ธ
list[2].number = 24;
strcpy(list[2].name, "ํ๊ธธ๋");
list[2].grade = 4.3;
}
๊ตฌ์กฐ์ฒด ๋ฐฐ์ด์ ์ด๊ธฐํ
struct student list[3] = {
{ 1, "Park", 3.42 },
{ 2, "Kim", 4.31 },
{ 3, "Lee", 2.98 },
};
๊ตฌ์กฐ์ฒด์ ํฌ์ธํฐ
struct student *p;
struct student s = { 24, "Kim", 4.3 };
struct student *p;
p = &s;
printf("ํ๋ฒ=%d ์ด๋ฆ=%s ํ์ =%f\n", s.number, s.name, s.grade);
printf("ํ๋ฒ=%d ์ด๋ฆ=%s ํ์ =%f\n", (*p).number, (*p).name, (*p).grade);
<์ผ๋ฐ์ ์ผ๋ก ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ>
struct student *p;
struct student s = { 24, "Kim", 4.3 };
struct student *p;
p = &s;
printf("ํ๋ฒ=%d ์ด๋ฆ=%s ํ์ =%f\n", s.number, s.name, s.grade);
printf("ํ๋ฒ=%d ์ด๋ฆ=%s ํค=%f\n", p->number, p->name, p->grade );
ํฌ์ธํฐ๋ฅผ ๋ฉค๋ฒ๋ก ๊ฐ์ง๋ ๊ตฌ์กฐ์ฒด
struct date{
int month'
int day;
int year;
};
struct student{
int number;
char name[20];
double grade;
struct date *dob;
};
int main(void)
{
struct date d = {3, 20, 1980};
struct student s = {20190001, "Kim", 4.3};
s.dob = &d;
printf("ํ๋ฒ : %d\n", s.number);
printf("์ด๋ฆ : %s\n", s.name);
printf("ํ์ : %f\n", s.grade);
printf("์๋
์์ผ : %d๋
%d์ %d์ผ\n", s.dob->year, s.dob->month, s.dob->day);
return 0;
}
๊ตฌ์กฐ์ฒด์ ํจ์
1. ๊ตฌ์กฐ์ฒด๋ฅผ ํจ์์ ์ธ์๋ก ์ ๋ฌํ๋ ๊ฒฝ์ฐ
- ๊ตฌ์กฐ์ฒด์ ๋ณต์ฌ๋ณธ์ด ํจ์๋ก ์ ๋ฌ๋๊ฒ ๋๋ค.
- ๋ง์ฝ ๊ตฌ์กฐ์ฒด์ ํฌ๊ธฐ๊ฐ ํฌ๋ฉด ๊ทธ๋งํผ ์๊ฐ๊ณผ ๋ฉ๋ชจ๋ฆฌ๊ฐ ์์๋๋ค.
int equal(struct student s1, struct student s2)
{
if( s1.number == s2.number )
return 1;
else
return 0;
}
int main(void)
{
sturct student a = {1, "hong", 3.8 };
sturct student b = {2, "kim", 4.0 };
if (equal(a, b) == 1 ){
printf("๊ฐ์ ํ์ \n");
}
else{
printf("๋ค๋ฅธ ํ์ \n");
}
}
2. ๊ตฌ์กฐ์ฒด์ ํฌ์ธํฐ๋ฅผ ํจ์์ ์ธ์๋ก ์ ๋ฌํ๋ ๊ฒฝ์ฐ
- ์๊ฐ๊ณผ ๊ณต๊ฐ์ ์ ์ฝํ ์ ์๋ค
- ์๋ณธ ํผ์์ ๊ฐ๋ฅ์ฑ์ด ์๋ค.
int equal(struct student *p1, struct student *p2)
{
if( p1->number == p2->number ) //ํฌ์ธํฐ๋ฅผ ํตํ์ฌ ๊ตฌ์กฐ์ฒด์ ์ ๊ทผํ๋ค
return 1;
else
return 0;
}
int main(void)
{
sturct student a = {1, "hong", 3.8 };
sturct student b = {2, "kim", 4.0 };
if (equal(&a, &b) == 1 ){
printf("๊ฐ์ ํ์ \n");
}
else{
printf("๋ค๋ฅธ ํ์ \n");
}
}
๊ตฌ์กฐ์ฒด๋ฅผ ๋ฐํํ๋ ๊ฒฝ์ฐ
- ๋ณต์ฌ๋ณธ์ด ๋ฐํ๋๋ค
// ๊ตฌ์กฐ์ฒด s๊ฐ ๊ตฌ์กฐ์ฒด a๋ก ๋ณต์ฌ๋๋ค
struct student create()
{
sturct student s;
s.number = 3;
strcpy(s.name "park");
s.grade=4.0;
return s;
}
int main(void)
{
struct student a;
a = create();
return 0;
}
'C Programming Language > C' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
C์ธ์ด ํฌ์ธํฐ์ ํ์ฉ : ์ด์ค ํฌ์ธํฐ, ํฌ์ธํฐ ๋ฐฐ์ด, ํจ์ ํฌ์ธํฐ (0) | 2024.08.02 |
---|---|
C์ธ์ด ๊ตฌ์กฐ์ฒด union, enum, typedef ๋? (0) | 2024.08.02 |
C์ธ์ด ๋ฌธ์์ด์ ์ฒ๋ฆฌํ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ํจ์ (0) | 2024.07.11 |
C์ธ์ด ๋ฌธ์์ ๋ฌธ์์ด์ ๋ํด ํ์ตํ๊ธฐ (0) | 2024.07.10 |
C์ธ์ด ํฌ์ธํฐ(pointer)์ ๊ฐ๋ ํ์ตํ๊ธฐ (0) | 2024.07.02 |