문 : How to interpret complex C/C++ declarations (http://www.codeproject.com/cpp/complex_declarations.asp)
int * (* (*fp1) (int) ) 10; 문 문 볼 ? 런 C/C++
문 면 를 떻 는를 . 매 본 범 복
루. 리 매 볼 는 문 롯 문 를 는 const typedef 및
를 룬 마막 "른 " 봅.
리 러 문 맞리 떻 문 는 방 는 .
복 문 를 는 밍 .
int a;
문 '변 n int ' .
문 보면,
int *p;
'변 p를 int * ' 말면 '변 p를 int 리는 '
. (*) 는 (&)는 변 (int) 붙는 보 변(p) 붙는
. 냐면 문 면 만 를 문. (: C++ Bjarne
Stroustrup는 변 붙는 더 .) (: B. S.는 더는 무 만 남
명 못 . 링를 면 더 를 보 . http://www.research.att.com/~bs/bs_faq2.html#whitespace )
int* p,q;
문 보 면 변 p 변 q 마 int를 리는 (int *) 변 럼 보. 만
변 p만 int를 리는 변q는 int . (: int *p; int *q; 럼 따
는 미 명 .)
리는 를 리는 를 .
char **argv;
리만 따면 는 무 복 . 따 리는 float 리는 를 리는 를 리는
를 . 몇 더 복 됩.
문들 봅. (: 부는 문 문 . 문
.)
int *q5;
변 p는 int 는 4 배 리는 (a pointer to an array of 4 ints)며, 변 q는 int 를
는 5 배(an array of 5 pointer to integers) .
const 는 변 변는 (변 <-> 변 ? 모 ) 는 . const 변
를 는 문 바 를 . 변 불능 문 문 는 .
const int n = 5;
int const m = 10;
변 n m const . C++ 모 능 만
는 const 미 더 명 번 문 .
const 면 복. 를 들 변 p, q .
const int *p;
int const *q;
럼 , const int 리는 int 리는 const 를 보.
변 모 const int를 리는 . int 리는 const 는 됩.
int * const r = &n; // n int 변
p q는 const int 변 문 *p *q 변 . r const 문
r = &m 른 는 불능. (물 m 른 int 변) 물 *r
변 능.
문 const int 리는 const 를 면 .
const int * const p = &n; // n const int 변
문들 보면 const를 떻 는 더 명 . 몇몇 문 면
동 만 는데 는 략. ( 딴 )
char ** p1; // pointer to pointer to char
const char **p2; // pointer to pointer to const char
char * const * p3; // pointer to const pointer to char
const char * const * p4; // pointer to const pointer to const char
char ** const p5; // const pointer to pointer to char
const char ** const p6; // const pointer to pointer to const char
char * const * const p7; // const pointer to const pointer to char
const char * const * const p8; // const pointer to const pointer to const char
typedef는 typedef는 "* 는 & 변 "는 복 . typedef 를 면
typedef char * PCHAR;
PCHAR p,q;
변 p, q 모 char를 리는 변 됩. typedef 면 q는 char를 리는 변 char 변 데 런 모르는 .
typedef를 문들 명 봅.
typedef char * a; // a is a pointer to a char
//
// a는
// char 리는
typedef a b(); // b is a function that returns
// a pointer to a char
//
// b는
// char 리는 를 리는
//
typedef b *c; // c is a pointer to a function
// that returns a pointer to a char
//
// c는
// char 리는 리는
// 를 리는
//
typedef c d(); // d is a function returning
// a pointer to a function
// that returns a pointer to a char
//
// d는
// char 리는 를 리는
// 를 리는
// 를 리는
//
typedef d *e; // e is a pointer to a function
// returning a pointer to a
// function that returns a
// pointer to a char
//
// e는
// char 리는 를 리는
// 를 리는
// 를 리는
// 를 리는
//
e var10; // var is an array of 10 pointers to
// functions returning pointers to
// functions returning pointers to chars.
//
// var는
// char 리는 를 리는
// 를 리는
// 를 리는
// 를 리는
// 를 는 10 배
typedef는 보 struct는 됩. struct 문 면 C++ 뿐만 C
struct를 략 .
typedef struct tagPOINT
{
int x;
int y;
}POINT;
POINT p; /* C */
마 문 는데 돈 는 . 는 DOS 백
램(TSR) 는데 Win32 X-Windows는 callback 를 는데 됩. 많
됩. 를 들면 블, STL 부 릿 리 Win NT/2K/XP 는
볼 . 럼 부 보.
int (*p)(char);
문 변 p를 char를 int를 리는 를 리는 (a pointer to a function that takes a char
argument and return an int) .
float를 char를 리는 를 리는 를 리는 를 리는 (a pointer to a
function that take two floats and returns a pointer to a pointer to a char)는 .
char ** (*p)(float, float);
면 char를 리는 const 를 받 void 를 리는 를 리는 를 는 5 배
(an array of 5 pointers to functions that receive two const pointers to chars and return void pointer) 떻 면
?
void * (*a5)(char * const, char * const);
매 만 복 문 .
문 부 . 른부 .
면 바뀌 . ( : " 바뀐" 미는 바를 . 는 를 는 는 른 들 는는 미 ). 모 내 면 밖
대. 모 문 복.
"Start reading the declaration from the innermost parentheses, go right, and then go left. When you encounter parentheses, the
direction should be reversed. Once everything in the parentheses has been parsed, jump out of it. Continue till the whole
declaration has been parsed."
른 변 : 문 는 부 는
변 부 .
문 번 명.
int * (* (*fp1) (int) ) 10;
1. 변 부 . --- fp1
2. 른 ) 말는 무 보면 * --- . 는 3. 리.
3. 를 벗 른 보면 (int) . --- 데 int를 받는.
4. 면 * . - 는 를 리는데 는 4.를 리.
5. 를 벗 른 보면 10 . --- 10 배.
6. 보면 * . --- 배 는 데 는 7.를 리.
7. 더 보면 int . --- int 리.
는 보 무 듭. 내 면 .
"fp1 int 리는 를 는 10 배 리는 를 리면 int를 받는 를 리
는 ".
1. Start from the variable name -------------------------- fp1
2. Nothing to right but ) so go left to find * -------------- is a pointer
3. Jump out of parentheses and encounter (int) --------- to a function that takes an int as argument
4. Go left, find * ---------------------------------------- and returns a pointer
5. Jump put of parentheses, go right and hit 10 -------- to an array of 10
6. Go left find * ----------------------------------------- pointers to
7. Go left again, find int -------------------------------- ints.
번 .
int *( *( *arr5)())();
1. 변 부 . --- arr
2. 른 보면 5 . --- 5 배.
3. 보면 * . --- 배 는 데 4.를 리.
4. 를 벗 른 보면 () . --- 데 를 무 받 .
5. 보면 * . --- 는 를 리는데 는 6. 리.
6. 를 벗 른 보면 () . --- 데 를 무 받 .
7. 보면 * . --- 는 를 리는데 는 8. 리.
8. 더 보면 int . --- int 리.
"arr int 리는 를 리면 무 받 는 를 리는 를 리면 무
받 는 를 리는 를 는 5 배".
1. Start from the variable name --------------------- arr
2. Go right, find array subscript --------------------- is an array of 5
3. Go left, find * ----------------------------------- pointers
4. Jump out of parentheses, go right to find () ------ to functions
5. Go left, encounter * ----------------------------- that return pointers
6. Jump out, go right, find () ----------------------- to functions
7. Go left, find * ----------------------------------- that return pointers
8. Continue left, find int ----------------------------- to ints.
( : 른- 따보면 대
맞 .)
float ( * ( *b()) [] )(); // b is a function that returns a
// pointer to an array of pointers
// to functions returning floats.
//
// () b는 데 를 리.
// 는 배 리는데 배 는 .
// 는 를 리는데 는 float를 리.
//
// () b는 float를 리는 를
// 리는 를 는 배 리는 를
// 리는 .
void * ( *c) ( char, int (*)()); // c is a pointer to a function that takes
// two parameters:
// a char and a pointer to a
// function that takes no
// parameters and returns
// an int
// and returns a pointer to void.
//
// () c는 데 는 를 리.
// 는 char
// int 리는 를 리는 를 .
// 는 를 리는데 는 void를 리.
//
// () c는 void를 리는 를 리면
// char int를 리는 를 리는 를 는
// 를 리는 .
void ** (*d) (int &,
char **(*)(char *, char **)); // d is a pointer to a function that takes
// two parameters:
// a reference to an int and a pointer
// to a function that takes two parameters:
// a pointer to a char and a pointer
// to a pointer to a char
// and returns a pointer to a pointer
// to a char
// and returns a pointer to a pointer to void
//
// () d는 데 는 를 리.
// 는 int 를 리는 를 .
// 리는 는
// char를 리는
// char를 리는 를 리는 를 면
// char를 리는 를 리는 를 리.
// 는 void를 리는 를 리는 를 리.
//
// () d는 void를 리는 를 리는 를 리면
// int
// char를 리는
// char를 리는 를 리는 를 면
// char를 리는 를 리는 를 리는
// 를 는 를
// 리는 .
// ~~~
float ( * ( * e10)
(int &) ) 5; // e is an array of 10 pointers to
// functions that take a single
// reference to an int as an argument
// and return pointers to
// an array of 5 floats.
//
// () e는 10 배데 는 .
// 는 를 리는데 는 int 를 받면
// 를 리.
// 는 5 배 리는데 배 는 float.
//
// () e는 float 는 5 배 리는
// 를 리면
// int 를 는 를 리는 를 는
// 10 배.
문는 명 만 멤 는 . 문 린
를 복.
// that takes no argument and return an int
//
// () p는 데 는 CFoo 멤를 리.
// 멤는 무런 받 int를 리.
//
// () p는 무런 받 int를 리는
// CFoo 멤 를 리는 .
맥 들 .
int (__stdcall* q)(); // q is a pointer to a __stdcall function
// that takes no argument and return int
//
// () q는 데 는 __stdcall 를 리.
// 는 무런 받 int를 리.
//
// () q는 무런 받 int를 리는
// __stdcall 를 리는 .
|}}
|}}