c语言typedef函数指针,函数指针的typedefine,迷惑,求教

函数指针的typedefine,不解,求教。

#include 

#include 

#include 

typedef int (*PipeProcessor)(void* args, int val);

typedef struct Pipe

{

struct Pipe* next;

PipeProcessor handler;

void* args;

} Pipe;

void init(Pipe* pipe)

{

pipe->next = NULL;

pipe->handler = NULL;

pipe->args = NULL;

}

void process(Pipe* first, int val)

{

Pipe* it = first;

while (it != NULL)

{

val = (*(it->handler))(it->args, val);

it = it->next;

}

}

void attach(Pipe* front, Pipe* next)

{

front->next = next;

}

int printPipe(void* args, int val)

{

printf("val = %d\n", val);

return val;

}

int addPipe(void* args, int val)

{

return (*(int*)(args)) + val;

}

int multiPipe(void* args, int val)

{

return (*(int*)(args)) * val;

}

int main()

{

Pipe first;

int first_args = 1;

Pipe second;

int second_args = 2;

Pipe third;

int third_args = 10;

Pipe last;

init(&first);

first.handler = addPipe;

first.args = &first_args;

init(&second);

second.handler = multiPipe;

second.args = &second_args;

attach(&first, &second);

init(&third);

third.handler = addPipe;

third.args = &third_args;

attach(&second, &third);

init(&last);

last.handler = printPipe;

attach(&third, &last);

process(&first, 1);

process(&first, 2);

process(&first, 3);

return 0;

}

用函数指针实现多态。我不理解的是这里typedefine 用法。 typedefing 不应该是  typedefine a b ; 这种形式么?:

typedef int (*PipeProcessor)(void* args, int val);

typedef struct Pipe

{

struct Pipe* next;

PipeProcessor handler;

void* args;

} Pipe;

------解决思路----------------------

一个函数的类型是用它的返回值来表示的,所以typedef定义函数类型时,就是

typedef int (*PipeProcessor)(void* args, int val);

这里int, 就是你说的a, *PipeProcessor就是你说的b, 因为是函数类型,所以后面有函数的参数类型定义

你可以和用typedef定义数组类型对比看,就容易明白了

typedef int A[5];

PipeProcessor前面的*, 你可以参考

typedef struct

{ int a; intb;

} STRU, *pSTRU;

pSTRU前面的*

------解决思路----------------------

typedef的用法我是这么理解,你把typedef盖掉,把后面的式子的声明分析一下。比如typedef int (*PipeProcessor)(void* args, int val);这里我们只看int (*PipeProcessor)(void* args, int val),那么我们可以知道PipeProcessor是一个函数指针,该指针指向的函数是一个返回值为int,参数为void *和int。然后我们就知道typedef是把PipeProcessor定义成了一个指向有两个参数(void *,int)返回值是int的函数指针类型。关键就是分析c语言的声明,这方法很多书里都有。

------解决思路----------------------

//char (*(*x[3])())[5];//x是什么类型的变量?

//

//分析C语言声明,关键是搞清楚这个变量是个什么东西(函数、指针、数组),

//是函数那么剩下的就是他的参数和返回值,

//是指针那剩下部分是说明他指向什么,

//是数组剩下的部分就是说明数组的成员是什么类型。

//解析C语言声明规则:

//从左侧第一个标识符开始,按照优先级进行结合。*表示是..的指针,const表示只读的,volatile表示可变的,[]表示是数组,()表示是函数。

//

//x和[3]结合说明是一个大小为3的数组,该数组的每个元素为一类指针,该类指针指向一类函数,该类函数无参数,返回一类指针,该类指针指向一个大小为5的char型数组

#include 

#include 

char num[5];

char (*x00())[5] {

return #

}

int main() {

char (*x000)[5];//返回值

char (*(x00)())[5];//函数原型,参数为空,返回值为指针

char (*(*x0)())[5];//数组的元素,是个函数指针

char (*(*x[3])())[5];//是个数组,大小为3

x0 = x00;

x[0] = x0;

x[1] = x0;

x[2] = x0;

printf("typeid(x).name() is %s\n",typeid(x).name());

return 0;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值