typedef的妙用
typedef給你一種方式來克服“*只適合于變量而不適合于類型”的弊端。你可以如下使用typedef:
typedef char * PCHAR;
PCHAR p,q;
這里的p和q都被聲明為指針。(如果不使用typedef,q將被聲明為一個char變量,這跟我們的第一眼感覺不太一致!)下面有一些使用typedef的聲明,并且給出了解釋:
typedef char * a;? // a is a pointer to a char
typedef a b();???? // b is a function that returns
?????????????????? // a pointer to a char
typedef b *c;????? // c is a pointer to a function
?????????????????? // that returns a pointer to a char
typedef c d();???? // d is a function returning
?????????????????? // a pointer to a function
?????????????????? // that returns a pointer to a 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 var[10];???????? // var is an array of 10 pointers to
?????????????????? // functions returning pointers to
?????????????????? // functions returning pointers to chars.
typedef經(jīng)常用在一個結(jié)構(gòu)聲明之前,如下。這樣,當創(chuàng)建結(jié)構(gòu)變量的時候,允許你不使用關(guān)鍵字struct(在C中,創(chuàng)建結(jié)構(gòu)變量時要求使用struct關(guān)鍵字,如struct tagPOINT a;而在C++中,struct可以忽略,如tagPOINT b)。
typedef struct tagPOINT
{
??? int x;
??? int y;
}POINT;
POINT p; /* Valid C code */
?
函數(shù)指針
函數(shù)指針可能是最容易引起理解上的困惑的聲明。函數(shù)指針在DOS時代寫TSR程序時用得最多;在Win32和X-Windows時代,他們被用在需要回調(diào)函數(shù)的場合。當然,還有其它很多地方需要用到函數(shù)指針:虛函數(shù)表,STL中的一些模板,Win NT/2K/XP系統(tǒng)服務(wù)等。讓我們來看一個函數(shù)指針的簡單例子:
int (*p)(char);
這里p被聲明為一個函數(shù)指針,這個函數(shù)帶一個char類型的參數(shù),并且有一個int類型的返回值。另外,帶有兩個float類型參數(shù)、返回值是char類型的指針的指針的函數(shù)指針可以聲明如下:
char ** (*p)(float, float);
那么,帶兩個char類型的const指針參數(shù)、無返回值的函數(shù)指針又該如何聲明呢?參考如下:
void * (*a[5])(char * const, char * const);
?
“右左法則”[重要!!!]
The right-left rule: 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.
這是一個簡單的法則,但能讓你準確理解所有的聲明。這個法則運用如下:從最內(nèi)部的括號開始閱讀聲明,向右看,然后向左看。當你碰到一個括號時就調(diào)轉(zhuǎn)閱讀的方向。括號內(nèi)的所有內(nèi)容都分析完畢就跳出括號的范圍。這樣繼續(xù),直到整個聲明都被分析完畢。
對上述“右左法則”做一個小小的修正:當你第一次開始閱讀聲明的時候,你必須從變量名開始,而不是從最內(nèi)部的括號。
下面結(jié)合例子來演示一下“右左法則”的使用。
int * (* (*fp1) (int) ) [10];
閱讀步驟:
1. 從變量名開始 -------------------------------------------- fp1
2. 往右看,什么也沒有,碰到了),因此往左看,碰到一個* ------ 一個指針
3. 跳出括號,碰到了(int) ----------------------------------- 一個帶一個int參數(shù)的函數(shù)
4. 向左看,發(fā)現(xiàn)一個* --------------------------------------- (函數(shù))返回一個指針
5. 跳出括號,向右看,碰到[10] ------------------------------ 一個10元素的數(shù)組
6. 向左看,發(fā)現(xiàn)一個* --------------------------------------- 指針
7. 向左看,發(fā)現(xiàn)int ----------------------------------------- int類型
總結(jié):fp1被聲明成為一個函數(shù)的指針的指針的數(shù)組,這個數(shù)組有10個元素,函數(shù)的原型為帶一個int類型的參數(shù),返回值為一個指針?
再來看一個例子:
int *( *( *arr[5])())();
閱讀步驟:
1. 從變量名開始 -------------------------------------------- arr
2. 往右看,發(fā)現(xiàn)是一個數(shù)組 ---------------------------------- 一個5元素的數(shù)組
3. 向左看,發(fā)現(xiàn)一個* --------------------------------------- 指針
4. 跳出括號,向右看,發(fā)現(xiàn)() -------------------------------- 不帶參數(shù)的函數(shù)
5. 向左看,碰到* ------------------------------------------- (函數(shù))返回一個指針
6. 跳出括號,向右發(fā)現(xiàn)() ------------------------------------ 不帶參數(shù)的函數(shù)
7. 向左,發(fā)現(xiàn)* --------------------------------------------- (函數(shù))返回一個指針
8. 繼續(xù)向左,發(fā)現(xiàn)int --------------------------------------- int類型
總結(jié):??
還有更多的例子:
float ( * ( *b()) [] )();????????????? // b is a function that returns a
?????????????????????????????????????? // pointer to an array of pointers
?????????????????????????????????????? // to functions returning floats.
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.
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
float ( * ( * e[10])
??? (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.
正文完
?