C언어/참고서: C언어 콘서트

[C언어 52차시] 구조체 정의, 변수, 선언, 초기화, 참조

Olivia-BlackCherry 2023. 4. 13. 09:12

1. 구조체 structure

여러 개의 기초 자료형으로 새로운 자료형을 정의할 수 있는 방법

객체지향프로그래밍의 '클래스' 개념의 모체이다. 

키워드: struct
구조체 태그: sturcture tag
소괄호: { }
구조체 멤버: structure member - 고유한 이름 갖는다.
세미콜론 ;
// 구조체 정의
struct plan {
	int days;
	char license[5];
	double score;
};

키워드: struct
구조체 태그: plan
소괄호: { }
구조체 멤버: days, license, score

세미콜론 ;

 

 

2. 구조체 변수

앞서 구조체 정의를 했지만, 이것은 변수는 아니기 때문에 어떤 값을 넣을 수 없다. 

따라서 변수를 정의해야 한다. 

아래의 march 변수의 크기는 대략 4+5+8= 17바이트이다.

(단, 컴파일러가 더 많은 메모리를 할당하는 경우도 있으므로 sizeof 연산자로 사이즈 크기를 정확히 계산해보자)

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
// 구조체 정의
struct plan {
	int days;
	char license[5];
	double score;
};

int main() {
	struct plan march;
	printf("%zd", sizeof(march)); //24바이트
	return 0;
}

구조체 변수: march

 

3. 구조체 초기화 = {}

요소들의 초기값을 중괄호 안에 나열한다.

struct plan march = { 31, "computer", 90.0};

 

 

4. 구조체 멤버 참조: ., strcpy()

멤버 연산자 . 을 쓴다.

march.days

march.license

march.score

 

값을 대입할 때, 숫자형일 때는 아래와 같이 쓰지만

march.days = 30;

 

문자열이라면 strcpy()를 사용한다.

strcpy(march.license, "data");

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 구조체 정의
struct plan {
	int days;
	char license[8];
	double score;
};

int main() {
	struct plan march;
	march.days = 30;
	strcpy(march.license, "computer");
	march.score = 90.0;

	printf("%d\n", march.days);
	printf("%s\n", march.license);
	printf("%lf\n", march.score);

	return 0;
}

>>30
computer
90.000000

 

 

5. 구조체 초기화

1) = {}

	struct plan march = { 30, "computer", 90.0 };

 

2)  {.멤버이름=,}

	struct plan march = { .days=30, .license="computer", .score=90.0 };

 

3) 구조체 복사

	struct plan april = march;
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 구조체 정의
struct plan {
	int days;
	char license[8];
	double score;
};

int main() {
	struct plan march = { .days=30, .license="computer", .score=90.0 };
	struct plan april = march;

	printf("%d\n", april.days);
	printf("%s\n", april.license);
	printf("%lf\n", april.score);

	return 0;
}

 

4)  = (struct 구조체태그) { }

	may = (struct plan){ 31, "data", 99.0 };
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 구조체 정의
struct plan {
	int days;
	char license[8];
	double score;
};

int main() {
	struct plan march = { .days = 30, .license = "computer", .score = 90.0 };
	struct plan april = march;
	struct plan may;
	may = (struct plan){ 31, "data", 99.0 };

	printf("%d\n", may.days);
	printf("%s\n", may.license);
	printf("%lf\n", may.score);

	return 0;
}

>>31
data
99.000000

 

이와 같이하면 구조체 변수 선언이 종료된 후에도 새롭게 초기화할 수 있기 때문이다. 

아래의 예시에서 구조체 plan의 변수 may는 april과 같은 값을 갖고 있었으나

새롭게 초기화되어 다른 값을 갖는다.

	struct plan march = { .days = 30, .license = "computer", .score = 90.0 };
	struct plan april = march;
	struct plan may = april;
	may = (struct plan){ 31, "data", 99.0 };

 

 

6. 연습문제 

- 두 점 사이의 거리 구하는 문제

1) 하나의 구조체에 구조체 멤버를 4개 설정

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
double point_distance(int, int, int, int);

// 구조체 정의
struct point {
	int x1;
	int y1;
	int x2;
	int y2;
};

int main() {
	struct point distance;
	scanf("%d %d", &distance.x1, &distance.x2);
	scanf("%d %d", &distance.y1, &distance.y2);

	printf("%d %d\n", distance.x1, distance.x2);
	printf("%d %d\n", distance.y1, distance.y2);

	return point_distance(distance.x1, distance.x2, distance.y1, distance.y2);
	
	return 0;
}

double point_distance(int x1,int x2,int y1, int y2)
{
	double result = sqrt(pow((x1 - x2), 2) + pow((y1 - y2), 2));
	printf("%lf", result);
	return result;
}

> x1, x2, y1,y2 10, 20, 10, 20 

>>>>14.142136

 

2) 두 개의 구조체에 구조체 멤버 각각 2개씩 설정

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
double point_distance(int, int, int, int);

// 구조체 정의
struct point {
	int x;
	int y;
};

int main() {
	struct point p,q;
	scanf("%d %d", &p.x, &p.y);
	scanf("%d %d", &q.x, &q.y);

	printf("%d %d\n", p.x, p.y);
	printf("%d %d\n", q.x, q.y);

	return point_distance(p.x, q.x, p.y, q.y);
	
	return 0;
}

double point_distance(int x1,int x2,int y1, int y2)
{
	double result = sqrt(pow((x1 - x2), 2) + pow((y1 - y2), 2));
	printf("%lf", result);
	return result;
}