구조체
여러 개의 변수를 묶어 하나의 객체를 표현하고자 할 때 구조체를 사용할 수 있습니다.
구조체를 활용해 학생정보를 출력하는 예시
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#include <stdio.h>
struct student {
char studentid[10];
char name[10];
int grade;
char major[100];
};
int main(void) {
struct student s;//구조체 변수 선언
strcpy(s.studentid, "123456");//구조체 변수에 접근
strcpy(s.name, "장병학");
s.grade = 1;
strcpy(s.major, "컴퓨터과학과");
printf("학번: %s\n", s.studentid);
printf("이름: %s\n", s.name);
printf("학년: %d\n", s.grade);
printf("학과: %s\n", s.major);
return 0;
}
|
cs |
구조체를 활용해 두 점 사이의 거리를 계산하는 예시
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
struct point {
int x;
int y;
};
int main(void) {
struct point p1, p2;
int xDiff, yDiff;
double distance;
printf("1번 점의 좌표를 입력하세요: ");
scanf("%d %d", &p1.x, &p1.y);
printf("2번 점의 좌표를 입력하세요: ");
scanf("%d %d", &p2.x, &p2.y);
xDiff = p1.x - p2.x;
yDiff = p1.y - p2.y;
distance = sqrt(xDiff*xDiff + yDiff * yDiff);
printf("두 점 사이의 거리는 %f입니다\n", distance);
return 0;
}
|
cs |
사각형의 넓이와 둘레를 구하는 프로그램
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
struct point {
int x;
int y;
};
struct rect {
struct point p1;
struct point p2;
};
int main(void) {
struct rect r;
int w, h, area, peri; //가로 세로 넓이 둘레
printf("왼쪽 상단의 좌표를 입력하세요 : ");
scanf("%d %d", &r.p1.x, &r.p1.y);
printf("오른쪽 하단의 좌표를 입력하세요 : ");
scanf("%d %d", &r.p2.x, &r.p2.y);
w = abs(r.p2.x - r.p1.x);
h = abs(r.p2.y - r.p1.y);
area = w * h;//넓이
peri = 2 * w + 2 * h;//둘레
printf("사각형의 넓이는 %d 이고, 둘레는 %d 입니다.", area, peri);
return 0;
}
|
cs |
구조체를 비교하는 예시
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
#include <stdio.h>
struct point {
int x;
int y;
};
void comparePoint(struct point p1, struct point p2) {
if ((p1.x == p2.x) && (p2.x == p2.y)) {
printf("p1과 p2가 같습니다");
}
}
int main(void) {
struct point p1;
struct point p2;
p1.x = 30;
p1.y = 10;
p2.x = 30;
p2.y = 30;
comparePoint(p1, p2);
return 0;
}
|
cs |
구조체의 변수를 한 번에 초기화하는 예시
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#include <stdio.h>
struct student {
char studentid[10];
char name[10];
int grade;
char major[100];
};
int main(void) {
struct student s = { "123456","장병학",4,"컴퓨터과학과" };
printf("학번: %s\n", s.studentid);
printf("이름: %s\n", s.name);
printf("학년: %d\n", s.grade);
printf("학과: %s\n", s.major);
return 0;
}
|
cs |
typedef 키워드를 이용하면 임의의 자료형을 만들 수 있으므로 선언을 더 짧아집니다
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
#include <stdio.h>
typedef struct student {
char studentid[10];
char name[10];
int grade;
char major[100];
} student;
int main(void) {
student s = { "123456","장병학",4,"컴퓨터과학과" };
printf("학번: %s\n", s.studentid);
printf("이름: %s\n", s.name);
printf("학년: %d\n", s.grade);
printf("학과: %s\n", s.major);
return 0;
}
|
cs |
구조체 포인터 변수에 접근하는 예시
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#include <stdio.h>
typedef struct student {
char studentid[10];
char name[10];
int grade;
char major[100];
} student;
int main(void) {
student *s = malloc(sizeof(student));
strcpy(s->studentid, "123456");
strcpy(s->name, "장병학");
s->grade = 4;
strcpy(s->major, "컴퓨터과학과");
printf("학번: %s\n", s->studentid);
printf("이름: %s\n", s->name);
printf("학년: %d\n", s->grade);
printf("학과: %s\n", s->major);
return 0;
}
|
cs |