标签:http color os io ar strong for 数据 div
函数指针是指向函数的指针变量。 因而“函数指针”本身首先应是指针变量,只不过该指针变量指向函数。这正如用指针变量可指向整型变量、字符型、数组一样,这里是指向函数。如前所述,C在编译时,每一个函数都有一个入口地址,该入口地址就是函数指针所指向的地址。有了指向函数的指针变量后,可用该指针变量调用函数,就如同用指针变量可引用其他类型变量一样,在这些概念上是一致的。函数指针有两个用途:调用函数和做函数的参数。
1
2
3
4
5
6
7
8
9
10
11
|
#include<stdio.h> intmax(intx,inty){ return (x>y?x:y);} intmain() { int (*ptr)( int , int ); inta,b,c; ptr=max; scanf ( "%d%d" ,&a,&b); c=(*ptr)(a,b); printf ( "a=%d,b=%d,max=%d" ,a,b,c); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
#include<stdio.h> voidFileFunc() { printf ( "FileFunc\n" ); } voidEditFunc() { printf ( "EditFunc\n" ); } voidmain() { typedefvoid(*funcp)(); funcppfun=FileFunc; pfun(); pfun=EditFunc; pfun(); } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
//指针函数是指返回值是指针的函数,即本质是一个函数 #include<iostream> usingnamespacestd; intmain() { float *find( float (*p)[4],intm); //查询序号为m的学生的四门课程的成绩 floatscore[][4]={{50,51,52,55},{70,70,40,80},{77,99,88,67}}; //定义成绩数组,第一维可以为变量 float *pf=NULL; //定义一个指针时一定要初始化 inti,m; cout<< "请输入您想查询的学生的序号:" ; cin>>m; pf=find(score,m); //返回为一维数组指针,指向一个学生成绩 for (i=0;i<4;i++) cout<<*(pf+i)<< "" ; cout<<endl; return0; } float *find( float (*p)[4],intm) { float *pf=NULL; pf=*(p+m); //p是指向二维数组的指针,加*取一维数组的指针 returnpf; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
#include"stdio.h" intadd1(inta1,intb1); intadd2(inta2,intb2); voidmain() { intnuma1=1,numb1=2; intnuma2=2,numb2=3; int (*op[2])(inta,intb); op[0]=add1; op[1]=add2; printf ( "%d%d\n" ,op[0](numa1,numb1),op[1](numa2,numb2)); } intadd1(inta1,intb1) { returna1+b1; } intadd2(inta2,intb2) { returna2+b2; } |
1
|
void (*intArray[])( void )={Stop,Run,Jump}; |
1
|
intArray[1](); //执行Run函数 |
1
2
3
4
|
typedefvoid(*intFunc)( void ); //此类型的函数指针指向的是无参、无返回值的函数。 intFuncintArray[32]; //定义一个函数指针数组,其每个成员为INTFUN类型的函数指针 intArray[10]=INT_TIMER0; //为其赋值 intArray[10](); //调用函数指针数组的第11个成员指向的函数 |
标签:http color os io ar strong for 数据 div
原文地址:http://www.cnblogs.com/meihao1989/p/3964616.html