: 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. 걸 고 급 기고
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 .
|}}
|}}