구조체란
타입이 다른 데이터를 하나로 묶는 방법
#include <stdio.h>
struct student {
int age;
char name[10];
char gender;
};
int main() {
struct student s1 = {14, "LJS", 'M'};
struct student s2 = {12, "KML", 'W'};
printf("%d %s, %c\n", s1.age, s1.name, s1.gender);
printf("%d %s, %c\n", s2.age, s2.name, s2.gender);
return 0;
}
// 14 LJS, M
// 12 KML, W
구조체는 다음의 모양으로 만들 수 있습니다.
struct 구조체명 {
변수1
변수2
....
};
사용할 때는 다음과 같이 사용 할 수 있습니다.
1. struct 구조체명 구조체변수명 = {초기값 설정};
2. struct 구조체명 구조체변수명;
struct student s1 = {14, "LJS", 'M'};
struct student s2;
typedef란
type에 별칭을 생성할 수 있음
long long a = 10;
typedef long long ll;
ll a = 10;
다음과 같이 long long을 ll이라는 별칭으로 지정해서 사용할 수도 있습니다.
구조체에서 만들어진 구조체를 다시 사용하기 위해서 아래의 형식을 사용합니다.
struct 구조체명 구조체변수명;
여기서 struct는 계속 반복해서 사용되는데 typedef를 통해서 구조체를 묶어주면 struct를 생략해서 사용할 수 있습니다.
#include <stdio.h>
typedef struct student {
int age;
char name[10];
char gender;
} stu;
int main() {
stu s1 = {14, "LJS", 'M'};
stu s2 = {12, "KML", 'W'};
printf("%d %s, %c\n", s1.age, s1.name, s1.gender);
printf("%d %s, %c\n", s2.age, s2.name, s2.gender);
return 0;
}
// 14 LJS, M
// 12 KML, W
'자료구조' 카테고리의 다른 글
배열(Array), 리스트(List) (0) | 2021.05.03 |
---|---|
동적 메모리 할당, malloc, calloc, realloc (0) | 2021.05.03 |
포인터, 이중 포인터, Call by value, Call by reference (0) | 2021.05.03 |
시간 복잡도, 공간 복잡도 (0) | 2021.05.03 |
[자료구조] 정렬 알고리즘 (0) | 2021.03.02 |