728x90
typedef
타입 이름을 직접 짓고 싶을 때 사용합니다.
예를 들어 int 타입 중 나이를 나타내는 타입을 Age로 따로 이름을 짓고 싶을 수 있습니다.
이런 경우 typedef를 사용할 수 있습니다.
사용법은 다음과 같습니다.
typedef int Age;
typedef char Name[20];
typedef char * Nickname;
예시
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int Age;
typedef double Height;
typedef double Weight;
typedef char Name[20];
typedef char * Nickname;
struct person
{
Name name;
Nickname nickName;
Age age;
Height height;
Weight weight;
struct person * another;
};
int main(int argc, char const *argv[])
{
struct person man1;
struct person man2;
man2.nickName = "광고!";
man1.another = &man2;
man1.nickName = "구독!";
man1.another->nickName = "구글광고!!!!!!";
printf("%s\n", man1.nickName);
printf("%s\n", man1.another->nickName);
return 0;
}
구조체를 typedef로 정의하기
위의 예시를 보셨다면 구조체도 typedef로 정의하고 싶을 수 있습니다.
구조체는 다음과 같이 typedef로 정의합니다.
typedef struct 구조체이름 지정할이름;
아래는 Person을 typedef로 정의한 예시입니다.
typedef struct person Person;
다음과 같이 사용합니다.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef int Age;
typedef double Height;
typedef double Weight;
typedef char Name[20];
typedef char * Nickname;
struct person
{
Name name;
Nickname nickName;
Age age;
Height height;
Weight weight;
struct person * another;
};
typedef struct person Person;
int main(int argc, char const *argv[])
{
Person man1;
Person man2;
man2.nickName = "광고!";
man1.another = &man2;
man1.nickName = "구독!";
man1.another->nickName = "구글광고!!!!!!";
printf("%s\n", man1.nickName);
printf("%s\n", man1.another->nickName);
return 0;
}
구조체 선언과 동시에 typedef 사용하기
위와 같이 구조체를 선언한 뒤 typedef를 사용하는 것은 매우 불편합니다.
이제 이를 편하게 사용하기 위한 방법을 알아보겠습니다.
기존 코드는 다음과 같았습니다.
struct person
{
Name name;
Nickname nickName;
Age age;
Height height;
Weight weight;
struct person * another;
};
typedef struct person Person;
다음과 같이 구조체 선언 전 typedef를 사용한 뒤, 마지막에 이름을 지정하여 사용할 수 있습니다.
typedef struct person //typedef 추가
{
Name name;
Nickname nickName;
Age age;
Height height;
Weight weight;
struct person * another;
} Person;//지정
그러나 구조체 내부에서 동일한 구조체를 사용할 경우 (위의 예시에서는 person * another),
이는 typedef로 이름을 정의하기 전이므로 위와 같은 방법으로밖에 설정할 수 없습니다.
다음과 같이 사용하면 에러가 발생합니다.
typedef struct person
{
Name name;
Nickname nickName;
Age age;
Height height;
Weight weight;
Person * another;//Person이 정의되기 전
} Person;
어차피 typedef를 사용하여 구조체를 정의해 줄 거라면, 구조체의 이름 자체가 필요 없는 상황일 수도 있습니다.
이런 경우에는 다음과 같이 구조체의 이름을 생략 가능합니다.
typedef struct //typedef 추가, 구조체 이름 생량
{
Name name;
Nickname nickName;
Age age;
Height height;
Weight weight;
} Person;//지정
그러나 이전 예시들 처럼, 자기 자신에 대한 구조체를 사용해야 하는 경우에는 생략할 수 없습니다.
즉 아래는 불가능합니다.
typedef struct
{
Name name;
Nickname nickName;
Age age;
Height height;
Weight weight;
struct person * another;//에러!!
} Person;
위와 같은 상황인 경우, 아래만이 가능합니다.
typedef struct person
{
Name name;
Nickname nickName;
Age age;
Height height;
Weight weight;
struct person * another;
} Person;
728x90
'c언어' 카테고리의 다른 글
[C언어] - Enum (열거형) (0) | 2022.06.15 |
---|---|
[C언어] - union (0) | 2022.06.12 |
[C언어] - 구조체 (Struct) (0) | 2022.06.12 |
[C언어] - 동적 메모리 할당 (malloc) (0) | 2022.06.12 |
[C언어] - 이중 포인터 (0) | 2022.05.06 |