Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 백준 2261번
- 트리(Tree)
- 백준 21606번
- 이분 그래프(Bipartite Graph)
- 분할 정복(Divide and Conquer)
- 그래프(Graph)
- BFS
- 이분 탐색(Binary Search)
- 알고리즘 개념
- 백준 17608번
- 위상 정렬(Topology Sort)
- 다익스트라 알고리즘(Dijkstra Algorithm)
- 백준 10000번
- DFS & BFS
- 백준 9012번
- 백준 18352번
- 백준 1707번
- 그리디 알고리즘(Greedy Algorithm)
- 스택(Stack)
- 백준 1948번
- 백준 2504번
- 큐(Queue)
- 백준 2812번
- 백준 2493번
- 위상 정렬(Topological Sort)
- 플로이드 워셜 알고리즘(Floyd-Warshall Algorithm)
- DFS
- BFS(Breadth First Search)
- DFS(Depth First Search)
- 동적 프로그래밍(Dynamic Programming)
Archives
- Today
- Total
Always Be Wise
typedef를 이용한 구조체 선언 본문
728x90
기본적으로 구조체를 선언할 때는 main 함수 밖에서 struct 구조체 이름 { 구조체 멤버들 }; 형태로 선언하고, main 함수에서
struct 구조체 이름 변수 이름 과 같이 선언한 후 사용한다. typedef를 이용하여 조금 더 편리하게 구조체 선언을 할 수 있다.
typedef는 C 언어에서 자료형을 새롭게 이름을 붙일 때 쓰는 키워드이다. typedef를 이용하면 main 함수에서 구조체를
선언할 때 매번 struct를 써줄 필요가 없다. 이 typedef를 사용할 때에는 구조체 별칭이 필요한데, 구조체 별칭은 아래와 같이
구조체를 정의할 때 중괄호 뒤에 써주면 된다.
#include <stdio.h>
typedef struct _Student {
int age;
char phone_number[14];
} Student; // 구조체 별칭
int main(){
Student goorm;
printf("나이 : ");
scanf("%d", &goorm.age);
printf("번호 : ");
scanf("%s", goorm.phone_number);
printf("----\n나이 : %d\n번호 : %s\n----", goorm.age, goorm.phone_number);
return 0;
}
구조체 이름을 적지 않고 구조체를 선언하고 사용하는 것도 가능하다. 구조체 이름을 따로 지정하지 않고 별칭만 사용하는 것을
익명 구조체라고 한다. typedef를 이용해 구조체 별칭을 사용할 때에도 문법은 똑같으므로 멤버에 접근할 때는 똑같이 .을 사용한다.
#include <stdio.h>
typedef struct {
int age;
char phone_number[14];
} Student;
int main(){
Student goorm;
printf("나이 : ");
scanf("%d", &goorm.age);
printf("번호 : ");
scanf("%s", goorm.phone_number);
printf("----\n나이 : %d\n번호 : %s\n----", goorm.age, goorm.phone_number);
return 0;
}
Comments