1. 问题
粉丝提问: 某个函数指针的使用:编译时出错。
键入 'int' in of 'dle'[--int] (*dle)();
风扇源码如下:
2.分析 1)结构分析
1 struct touch_message
2 {
3 rt_uint16_t x;
4 rt_uint16_t y;
5 rt_uint8_t event;
6 };
7 typedef struct touch_message * touch_message_t;
8 typedef (*on_touch_messgae_handle)(touch_message_t);
首先看第7行的类型定义:
typedef struct touch_message * touch_message_t;
定义后
touch_message_t
相当于
struct touch_message *
也就是说,如果我们定义的变量是一个类型的指针。
我们来分析一下接下来8行的定义:
typedef (*on_touch_messgae_handle)(touch_message_t);
可以用下面的定义代替
typedef (*on_touch_messgae_handle)(struct touch_message *);
2)逐步分析
有些C语言基础不好的朋友可能一眼就看不出来这个定义。 为了让新手更容易理解,我们先看一下下面的递进定义:
int fun;
这是一个整型变量fun;
int fun();
这是一个有趣的函数,参数:空返回值:int类型
int fun(struct touch_message *);
这是一个函数fun,参数:*的指针返回值:int类型
上述变化很容易理解。 接下来我们将fun修改如下:
int (*fun)(struct touch_message *);
括号具有最高优先级。 一旦这样定义了(fun),fun就必须先和fun结合起来,这样fun就变成了一个指针。
那么这个指针指向什么呢? 你需要看看它外面是怎么定义的,右边是( * ),左边是int,所以表示指针指向一个函数,
参数:指向 * 的指针 返回值:int 类型
示例:将一个函数分配给函数指针fun。 int (*){}int (*fun)(*);fun = ;
这里有一个隐藏的知识点。 函数名实际上是一个地址,赋值时函数类型必须与函数指针类型一致。
typedef int (*fun)(struct touch_message *);
如果添加左侧呢? 相当于给fun设置了一个新的类型,我们可以用fun来定义一个函数指针,函数类型同上。
例如,定义一个新类型的函数指针变量并为其赋值。 int (*fun)( *);int (*){}fun ; = ;
然后修改参数为,得到风扇源码中的外观,
typedef int (*fun)(touch_message_t);
不过粉丝源码中定义的函数类型缺少函数返回值的描述,所以在左边添加一个int或者其他类型就可以了。
3.函数指针
函数指针在linux内核中使用非常频繁,
比如对于字符设备,内核为很多字符设备提供了统一的接口。 我们对设备的所有操作都被抽象为读、写、打开、关闭等,并封装成一个结构体:
struct file_operations {
struct module *owner;
loff_t (*llseek) (struct file *, loff_t, int);
ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t);
int (*iterate) (struct file *, struct dir_context *);
unsigned int (*poll) (struct file *, struct poll_table_struct *);
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
int (*mmap) (struct file *, struct vm_area_struct *);
int (*open) (struct inode *, struct file *);
int (*flush) (struct file *, fl_owner_t id);
int (*release) (struct inode *, struct file *);
int (*fsync) (struct file *, loff_t, loff_t, int datasync);
int (*aio_fsync) (struct kiocb *, int datasync);
int (*fasync) (int, struct file *, int);
int (*lock) (struct file *, int, struct file_lock *);
ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
int (*check_flags)(int);
int (*flock) (struct file *, int, struct file_lock *);
ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
int (*setlease)(struct file *, long, struct file_lock **);
long (*fallocate)(struct file *file, int mode, loff_t offset,
loff_t len);
int (*show_fdinfo)(struct seq_file *m, struct file *f);
};
那么我们应该如何定义结构体变量并初始化它呢?
static struct file_operations hello_ops =
{
.open = hello_open,
.release = hello_release,
.read = hello_read,
.write = hello_write,
};
函数定义如下:
static int hello_open (struct inode *inode, struct file *filep)
{
return 0;
}
static int hello_release (struct inode *inode, struct file *filep)
{
return 0;
}
static ssize_t hello_read (struct file *filep, char __user *buf, size_t size, loff_t *pos)
{
return size;
}
static ssize_t hello_write (struct file *filep, const char __user *buf, size_t size, loff_t *pos)
{
return size;
}
注意,函数的参数和返回值必须严格按照结构体中的类型定义。
加入优质粉丝群,赶紧加彭老师为好友,长按【】,拉你进群!
-结尾-
关注并回复【1024】海量Linux资料大礼包
精彩文章合集