문 : 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 를 가리는 다.
|}}
|}}