[완료]Pointer to function 부분에서 갑자기 혼란이 옵니다!
글쓴이: soratz / 작성시간: 수, 2011/02/09 - 9:10오후
안녕하세요
APUE 책을 공부하고 있는데요
문득 제 머릿속의 개념이 흔들리는 부분이 있어서
책과 인터넷을 찾아 보았으나
해결이 안되서 이렇게 질문 올립니다.
signal 부분을 보다보니
#define SIG_ERR (void (*) ())-1
#define SIG_DFL (void (*) ())0
#define SIG_IGN (void (*) ())1
이렇게 선언된 부분이 있고
"pointer to a function that takes an integer argument and returns nothing"
이렇게 되있는데요
정수를 받고 void를 리턴하는 함수라면
(void (*) (int)) 이렇게 되야 하는것 아닌지요
저 뒤에 있는 -1, 0, 1 부분이 잘 이해가 되지 않습니다.
그리고 signal 함수의 prototype을 보면
void (*signal(int signo, void (*func)(int)))(int);
이렇게 선언되어있는데
리턴값이 void를 리턴하고 int형 인자를 받는 function 인데요.
직관적으로 제일뒤에 붙은 (int)가 이해가 잘 안되네요
혹시 이 부분에 대해 참조할만한 곳이 있을까요?
부끄럽지만 시간이 되시는 분이 계시면 조언 부탁드립니다 ^^
Forums:
이런 답변이 있네요.
//
http://kldp.org/node/4103
//
답변이 맞는건 같은데;;;;. 알면. 참 좋을텐데... 영어해석이 어렵네요.
http://www.velocityreviews.com/forums/t715007-re-how-to-understand-this-cpp-syntax.html
#define SIG_ERR ((void (*) ())-1)
#define SIG_DFL ((void (*) ())0)
#define SIG_IGN ((void (*) ())1)
Each definition is an integer constant expression converted,
via a cast, to a pointer-to-function type. The cast is to type
``void(*)()'', i.e., pointer to function returning void.
A modern implementation would probably define the
parameter type:
#define SIG_ERR ((void (*) (int))-1)
#define SIG_DFL ((void (*) (int))0)
#define SIG_IGN ((void (*) (int))1)
SIG_ERR, SIG_DFL, and SIG_IGN have to be function pointers, but
they merely have to be unique values; it needn't be possible to
call anything using their values. Converting a constant 0 to a
pointer-to-function type yields a null pointer, so SIG_DFL happens
to expand to an expression that evaluates to a null pointer value;
(SIG_DFL == NULL) is true. Converting any other integer value to
a pointer-to-function type yields some value that's not specified
by the standard (I'm not sure whether it's implementation-defined
or unspecified). The point is that, for the implementation for
which the above definitions were written, the three expressions
yield pointer values that are distinguishable from any pointer to
an actual function.
----------------------------------------------------------------------------
젊음'은 모든것을 가능하게 만든다.
매일 1억명이 사용하는 프로그램을 함께 만들어보고 싶습니다.
정규 근로 시간을 지키는. 야근 없는 회사와 거래합니다.
각 분야별. 좋은 책'이나 사이트' 블로그' 링크 소개 받습니다. shintx@naver.com
정말 감사합니다.
제가 미처 함수로는 검색을 못해봤네요 죄송하고 감사합니다.
파라미터가 정해지지 않은 함수 포인터도 있다는 사실에
새삼 놀랍습니다.
그리고 정수가 함수로 캐스팅이 되는것도 몰랐네요
제 무지를 심히 반성합니다 ㅠ
좀 더 고급 책을보면 나오는 내용인가봐요
아무튼 너무 감사합니다 !
댓글 달기