U E D R , A S I H C RSS

C/C++어려운선언문해석하기

{{|
CodeProject 미로 다. 글 로 길 보라 각되 날림다. 반대라런 경 록 같 다.



문 : 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 RollNum304;

int (*p)4=RollNum;

int *q5;



p는 int 기가 4 가리(a pointer to an array of 4 ints)며, 변 q는 int

기가 5(an array of 5 pointer to integers) 다.





const modifier

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 를 게 되면



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다.





똑같 다는 다. 달린 답글

를 복다.



int (CFoo::*p)(); // p is a pointer to a method in class CFoo

// that takes no argument and return an int

//

// () p는 CFoo 를 가리다.

// 무런 고 int를 리다.

//

// () p는 무런 고 int를 리

// CFoo 를 가리다.



(calling convetion)

맥락 똑같 다.



int (__stdcall* q)(); // q is a pointer to a __stdcall function

// that takes no argument and return int

//

// () q는 는 __stdcall 를 가리다.

// 무런 고 int를 리다.

//

// () q는 무런 고 int를 리

// __stdcall 를 가리다.
|}}

Valid XHTML 1.0! Valid CSS! powered by MoniWiki
last modified 2021-02-07 05:22:47
Processing time 0.0525 sec