题目,关于: typedef int (* func)(int ,int )
来源:互联网 发布:淘宝卖家的钱在哪儿看 编辑:程序博客网 时间:2024/05/01 00:45
例题:
#include<stdio.h>
int inc(int a)
{
return(++a);
}
int multi(int*a,int*b,int*c)
{
return(*c=*a**b);
}
typedef int(FUNC1)(int);
typedef int(FUNC2) (int*,int*,int*);
void show(FUNC2 fun,int arg1, int *arg2)
{
//fun 为指向函数int multi(int*a,int*b,int*c)的指针
//arg1 = 10,arg2 =&a;
FUNC1 *p = &inc; //p为指向函数inc的指针
int temp =p(arg1); //等价于 temp = inc(arg1) = inc(10) = 11
fun(&temp,&arg1, arg2);
//运行fun()函数后,*arg2 = temp * arg1 = 11 * 10 = 110
printf("%d\n",*arg2);
}
int main()
{
int a;
show(multi,10,&a);
return 0;
}
结果:110
先看一下例子:
#include "iostream"
using namespace std;
int add(int a,int b){
return (a+b);
}
typedef int (* func)(int ,int ) ;
void main(){
func f = add;
int n = f(1,2);
cout << n << endl;
}
其中 typedef int (* func)(int ,int ) 比较难理解,因为我们知道typedef 是宏定义,一般的语法是这样:
typedef unsigned long ulong;
这个定义是用ulong作unsigned long 的别名
在这里的意思是定义一种指针类型lpAddFun,它是一种指向函数int (int,int)的指针,也就是说lpAddFun表示的是这种类型的函数的地址,因为:
函数名不是其类型的一部分函数的类型只由它的返回值和参数表决定.指向add()的指针必须指向与add()相同类型的函数带有相同的返回类型和相同的参数表 。
int (* func)(int ,int )和int* func(int ,int )是不同的,后者说明的是返回一个int类型的指针,所以括号是必须加的
- 题目,关于: typedef int (* func)(int ,int )
- typedef int (* func)(int ,int )
- typedef int (* func)(int ,int )
- typedef的用法之 typedef int (* func)(int ,int )
- int (*func(int)) (int *,int)
- typedef的使用...typedef int (*Func)(int,int); Func func = sum;
- 关于typedef int (*pFunc)(int, int)
- 关于typedef int(*lpAddFun)(int, int)
- 关于typedef int (*ADDPROC)(int a, int b)
- typedef int (* func)(int ,int ) 定义函数指针类型 的理解
- 关于typedef int ( ) ( )的用法
- int const* func(const int&) | const int* func(const int&) | int *func() const区别
- type void func(int)
- typedef int (*pfunc)(int ,int);是什么?
- 关于c语言声明 void (*signal(int sig, void(*func)(int)))(int)的理解
- int *func(int, int) 和int (*func1)(int ,int)的区别
- Func<int, int, string>返回string
- int
- Fibonacci
- Josephus Problem
- 字符串的操作
- cf419 (div2)3/5
- Android送花动画
- 题目,关于: typedef int (* func)(int ,int )
- 【机器学习算法模型】分类算法——贝叶斯网络
- 软件工程(C编码实践)学习总结
- 数独生成算法设计
- 手持式RFID终端机工作原理及应用行业
- Python3爬虫简单样例
- linux的基础知识
- Joseph
- 安卓开发入门之图片缩放