C 언어에서 typedef 와 struct 를 구분하는 이유

amorette의 이미지


typedef struct sth_t {
int x;
int y;
} sth;

이렇게 하지 않고,

struct sth_t {
int x;
int y;
};
typedef struct sth_t sth;

이렇게 typedef 문과 struct 문을 반드시 분리하라는 권고를 자주 본 것 같습니다.
많은 프로젝트 코드도 그렇게 되어 있습니다.

이유가 있다면 무엇인가요?

cinsk의 이미지

글쎄요, 문법적인 부분에서 다른 점은 없습니다, 순전히 스타일 문제인 것 같네요. 기존 코드가 쓰는 방식 대로 하시는 것이 좋을 것 같네요.

--
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
Korean Ver: http://www.cinsk.org/cfaqs/

empty2fill의 이미지

typedef struct node_t {

int data;
node_t *next;

} node_t;

뭐라 설명해야 할지 정확안 용어는 모르겠네요.

컴파일러가 node_t 타입을 정의(확인)하는 도중 또 node_t가 나오는 경우 입니다.

이건 forward declaration 문제 일 수 도 있겠네요.

——
———
Life is a tragedy when seen in close-up, but a comedy in long-shot. - Chaplin, Charlie -

haze11의 이미지

각 헤더에서 서로 사용 할 때도 유용합니다.
또한 .h 에 typedef 하고 .c에 struct 선언하면
private/protect 와 같은 효과도.. =.=