函数指针

来源:互联网 发布:oracle数据库集群方案 编辑:程序博客网 时间:2024/06/16 02:05

函数指针在DOS时代写TSR程序时用的最多;在Win32和X-Windows时代,它们被用在需要回调函数的场合。当然还有很多其他地方需要用到函数指针:虚函数表,STL中的一些模板,Win NT/2K/XP系统服务等。

 

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 declaration should be reversed. Once everything in the parentheses has been parsed, jump out of it. Continue till the whole declaration was parsed.

从最内部的括号开始阅读声明,向右看,然后向左看。当你碰到一个括号时就调转阅读的方向。括号内的所有内容都分析完毕就跳出括号的范围。这样继续,直到整个声明都被分析完毕。

 

对上述的“右左法则”做一个小小的修正:当你第一次开始阅读声明的时候,你必须从变量名开始,而不是从最内部的括号。

 

下面结合一个例子来演示一下“右左法则”的使用:

 

int * ( * (*fp1) (int) ) [10];

 

 

阅读步骤:  

1. 从变量名开始 -------------------------------------------- fp1  

2. 往右看,什么也没有,碰到了),因此往左看,碰到一个* ------ 一个指针  

3. 跳出括号,碰到了(int) ----------------------------------- 一个带一个int参数的函数  

4. 向左看,发现一个* --------------------------------------- (函数)返回一个指针  

5. 跳出括号,向右看,碰到[10] ------------------------------ 一个10元素的数组  

6. 向左看,发现一个* --------------------------------------- 指针  

7. 向左看,发现int ----------------------------------------- int类型  

.

 

总结:fp1被声明成为一个函数的指针,该函数返回指向指针数组的指针. 

 

 

 

再来看一个例子:  

 

int *( *( *arr[5])())();  

 

阅读步骤:  

1. 从变量名开始 -------------------------------------------- arr  

2. 往右看,发现是一个数组 ---------------------------------- 一个5元素的数组  

3. 向左看,发现一个* --------------------------------------- 指针  

4. 跳出括号,向右看,发现() -------------------------------- 不带参数的函数  

5. 向左看,碰到* ------------------------------------------- (函数)返回一个指针  

6. 跳出括号,向右发现() ------------------------------------ 不带参数的函数  

7. 向左,发现* --------------------------------------------- (函数)返回一个指针  

8. 继续向左,发现int --------------------------------------- int类型  

 

总结:arr被声明成为一个函数的数组指针,该函数返回指向函数指针的指针。?? 

 

 

 

还有更多的例子:  

 

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.   

 

 

 

 

指针数组,数组指针,函数指针。。。

 

int *(*p(int))[3];

//P 开始,先与()结合,说明P 是一个函数,然后进

//()里面,int 结合,说明函数有一个整型变量

//参数,然后再与外面的*结合,说明函数返回的是

//一个指针,,然后到最外面一层,先与[]结合,说明

//返回的指针指向的是一个数组,然后再与*结合,

//明数组里的元素是指针,然后再与int 结合,说明指

//针指向的内容是整型数据.所以P 是一个参数为一个

//整数据且返回一个指向由整型指针变量组成的数组

//的指针变量的函数.

原创粉丝点击