1。粉丝提问:某函数指针的使用:编译时出错。在“on_touch_messgae_handle”[-Wimplicit-int]typedef(*on_touch_messgae_handle)(touch_message_t)的声明中类型默认为“int”;Fan源码如下:二、分析1)结构分析1typetouch_message2{3rt_uint16_tx;4rt_uint16_ty;7rt_uint8_str_m}*touch_message_t;8typedef(*on_touch_messgae_handle)(touch_message_t);先看下7行的类型定义:typedefstructtouch_message*touch_message_t;定义后touch_message_t等同于structtouch_message*也就是说,如果我们用touch_message_t来定义一个structtouch_message类型的变量。我们来分析下8行的定义:typedef(*on_touch_messgae_handle)(touch_message_t);可以用下面的定义替换typedef(*on_touch_messgae_handle)(structtouch_message*);这个定义不是一眼就能看出来的。为了让新手更容易理解,我们来看一下下面递进的定义:intfun;这是一个整型变量fun;内部函数();这是一个fun函数,参数:空返回值:int类型intfun(structtouch_message*);这是一个函数fun,参数:structtouch_message的指针*返回值:int类型上面的变化很容易理解,我们把fun修改成:int(*fun)(structtouch_message*);括号具有最高优先级。一旦(fun)这样定义,fun就必须先和fun结合,所以fun就变成了一个指针,那么这个指针指向什么呢?你需要看看外面是怎么定义的。右边是(structtouch_message*),左边是int,所以表示指针指向一个函数,参数:structtouch_message*的指针返回值:int类型例子:将函数my_fun赋值给函数指针乐趣。intmy_fun(structtouch_message*){}int(*fun)(structtouch_message*);乐趣=我的乐趣;这里有个隐藏的知识点,函数名其实就是一个地址,函数类型必须和函数指针类型一致赋值时一致。typedefint(*有趣)(structtouch_message*);如果左边加了typedef呢?相当于把fun设置为一个新的类型。我们可以使用fun来定义一个函数指针,函数类型同上。示例定义一个具有新类型的函数指针变量并为其赋值。typedefint(*fun)(structtouch_message*);intmy_fun(structtouch_message*){}funfun_ptr;fun_ptr=my_fun;然后修改参数为touch_message_t,即可得到粉丝源码,typedefint(*fun)(touch_message_t);但是范源码中定义的函数类型缺少函数返回值的描述,所以在左边加一个int或者其他类型即可。3、函数指针函数指针在Linux内核中的使用非常频繁,比如字符设备。内核为许多字符设备提供了统一的接口。所有对设备的操作都被抽象为读、写、打开、关闭等,并封装到结构体structfile_operations中:structfile_operations{structmodule*owner;loff_t(*llseek)(structfile*,loff_t,int);ssize_t(*read)(structfile*,char__user*,size_t,loff_t*);ssize_t(*write)(structfile*,constchar__user*,size_t,loff_t*);ssize_t(*aio_read)(structkiocb*,conststructiovec*,unsignedlong,loff_t);ssize_t(*aio_write)(structkiocb*,conststructiovec*,unsignedlong,loff_t);int(*iterate)(structfile*,structdir_context*);unsignedint(*poll)(structfile*,structpoll_table_struct*);long(*unlocked_ioctl)(structfile*,unsignedint,unsignedlong);long(*compat_ioctl)(structfile*,unsignedint,unsignedlong);int(*mmap)(structfile*,structvm_area_struct*);int(*open)(structnode*,structfile*);int(*flush)(structfile*,fl_owner_tid);int(*release)(structnode*,structfile*);int(*fsync)(structfile*,loff_t,loff_t,intdatasync);int(*aio_fsync)(structkiocb*,intdatasync);int(*fasync)(整数,海峡uctfile*,int);int(*lock)(structfile*,int,structfile_lock*);ssize_t(*sendpage)(structfile*,structpage*,int,size_t,loff_t*,int);unsignedlong(*get_unmapped_area)(structfile*,unsignedlong,unsignedlong,unsignedlong,unsignedlong);int(*check_flags)(int);int(*flock)(structfile*,int,structfile_lock*);ssize_t(*splice_write)(structpipe_inode_info*,structfile*,loff_t*,size_t,unsignedint);ssize_t(*splice_read)(structfile*,loff_t*,structpipe_inode_info*,size_t,unsignedint);int(*setlease)(structfile*,long,structfile_lock**);long(*fallocate)(structfile*file,intmode,loff_toffset,loff_tlen);int(*show_fdinfo)(structseq_file*m,structfile*f);};那我们应该如何确定结构变量并开始化呢?staticstructfile_operationshelo_ops={.open=hello_open_,.release=hello,.read=hello_read,.write=hello_write,};函数定义如下:staticinthello_open(structinode*inode,structfile*filep){return0;}staticinthello_release(structinode*inode,structfile*filep){return0;}staticssize_thello_read(structfile*filep),字符__用户*buf,size_tsize,loff_t*pos){returnsize;}staticssize_thello_write(structfile*filep,constchar__user*buf,size_tsize,loff_t*pos){returnsize;}注意函数的参数和返回值必须严格按照用结构体structfile_operations【编者推荐】巩固JS可选(?.)运算符号。原来函数也可以写成optional,又学习了!学习C++指针声明和指针相关概念前端问题——原型、构造函数和实例之间的奇妙关系Python函数式编程&模块分析这个类库可以帮助你理解Java中的函数式编程
