2010-07-28 5 views
1

typedef void (콜백) (int * p1, sStruct * p2);C에서 반복적으로 선언 함

typedef struct _sStruct 
{ 
callback *funct; 
}sStruct; 

다음과 같은 선언이 C에 있습니다.이 반복 선언을 오류없이 컴파일하려면 어떻게해야합니까?

잠시 동안 : '*'앞에 구문 오류가 있습니다. 첫 번째 줄에는 토큰이 있습니다.

/* Tell the compiler that there will be a struct called _sStruct */ 
struct _sStruct; 

/* Use the full name "struct _sStruct" instead of the typedef'ed name 
    "sStruct", since the typedef hasn't occurred yet */ 
typedef void (callback)(int *p1, struct _sStruct *p2); 

/* Now actually define and typedef the structure */ 
typedef struct _sStruct 
{ 
    callback *funct; 
} sStruct; 

편집 : 유형 이름의 문제의 변화에 ​​맞게 업데이트

+0

"반복적 선언"이란 무엇입니까? 원형 선언 또는 자체 참조를 의미합니까? – progrmr

답변

11

당신은 구조를 전달-선언 할 수 있습니다.

또한 구조체에 _sStruct 식별자를 지정하지 않는 것이 좋습니다. _으로 시작하는 전역 이름은 예약 된 이름이며 사용자 고유의 식별자로 사용하면 정의되지 않은 동작이 발생할 수 있습니다.

+1

또한 'typedef struct sStruct sStruct;'와 같이 한 번에'typedef'를 선언 할 수 있습니다. 그리고 struct와'typedef'에 같은 이름을 주저하지 마십시오. –

관련 문제