函数指针定义的一个错误

开发 前端
字符设备,内核给多有的字符设备提供了一个统一的接口,我们对设备的所有操作被抽象成read、write、open、close等。

[[405982]]

1. 问题

粉丝提问:某个函数指针的使用:编译时出错了。

type defaults to 'int' in declaration of 'on_touch_messgae_handle'[-Wimplicit-int] typedef(*on_touch_messgae_handle)(touch_message_t);

粉丝源码如下:

2. 分析

1) 结构解析

  1. 1 struct touch_message                                                         
  2.  2 { 
  3.  3     rt_uint16_t x; 
  4.  4     rt_uint16_t y;   
  5.  5     rt_uint8_t event;    
  6.  6 }; 
  7.  7 typedef struct touch_message * touch_message_t; 
  8.  8 typedef (*on_touch_messgae_handle)(touch_message_t); 

首先看下7行这个类型定义:

  1. typedef struct touch_message * touch_message_t; 

定义后

  1. touch_message_t  

等价于

  1. struct touch_message * 

就是说我们如果用touch_message_t 定义的变量是一个struct touch_message类型的一个指针。

再来分析下8行这个定义:

  1. typedef (*on_touch_messgae_handle)(touch_message_t); 

可以替换成下面这个定义

  1. typedef (*on_touch_messgae_handle)(struct touch_message *); 

2) 分步解析

有的C语言基础不是很好的朋友,可能无法一眼看出来这个定义, 为了让新手更容易看懂,我们来看一下下面一个递进式的定义:

  1. int fun; 

这是一个整型变量fun;

  1. int fun(); 

这是一个函数fun, 参数 :空 返回值:int型

  1. int fun(struct touch_message *); 

这是一个函数fun, 参数 :struct touch_message *的一个指针 返回值:int型

上述的变化都好理解,下面我们将fun做如下修改:

  1. int (*fun)(struct touch_message *); 

括号的优先级最高,(fun)一旦如此定义,那么fun就要先和结合, 所以fun变成了一个指针,

那么该指针指向什么呢?就需要看外面是如何定义的, 右边是(struct touch_message * ),左边是int, 所以说明指针指向的是一个函数,

参数 :struct touch_message *的一个指针 返回值:int型

举例:将函数my_fun赋值给函数指针fun。int my_fun(struct touch_message *) { } int (*fun)(struct touch_message *); fun = my_fun;

这里有一个隐藏的知识点,函数名其实也是一个地址,而且赋值的时候函数类型必须和函数指针类型一致。

  1. typedef int (*fun)(struct touch_message *); 

如果左边再加上typedef呢?相当于是设置fun为新的类型,我们可以用fun来定义一个函数指针, 该函数类型同上。

举例 用新的类型定义一个函数指针变量,并给他赋值。typedef int (*fun)(struct touch_message *); int my_fun(struct touch_message *) { } fun fun_ptr; fun_ptr = my_fun;

然后将参数修改为,touch_message_t,就得到了粉丝的源码中的样子,

  1. typedef int (*fun)(touch_message_t); 

但是粉丝的源码中定义的函数类型缺少了对函数返回值的描述,所以左侧增加一个int或者其他类型即可即可。

3. 函数指针

函数指针在linux内核中使用非常频繁,

比如字符设备,内核给多有的字符设备提供了一个统一的接口,我们对设备的所有操作被抽象成read、write、open、close等,并封装到结构体struct file_operations 中:

  1. struct file_operations { 
  2.  struct module *owner; 
  3.  loff_t (*llseek) (struct file *, loff_t, int); 
  4.  ssize_t (*read) (struct file *, char __user *, size_t, loff_t *); 
  5.  ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *); 
  6.  ssize_t (*aio_read) (struct kiocb *, const struct iovec *, unsigned long, loff_t); 
  7.  ssize_t (*aio_write) (struct kiocb *, const struct iovec *, unsigned long, loff_t); 
  8.  int (*iterate) (struct file *, struct dir_context *); 
  9.  unsigned int (*poll) (struct file *, struct poll_table_struct *); 
  10.  long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long); 
  11.  long (*compat_ioctl) (struct file *, unsigned int, unsigned long); 
  12.  int (*mmap) (struct file *, struct vm_area_struct *); 
  13.  int (*open) (struct inode *, struct file *); 
  14.  int (*flush) (struct file *, fl_owner_t id); 
  15.  int (*release) (struct inode *, struct file *); 
  16.  int (*fsync) (struct file *, loff_t, loff_t, int datasync); 
  17.  int (*aio_fsync) (struct kiocb *, int datasync); 
  18.  int (*fasync) (int, struct file *, int); 
  19.  int (*lock) (struct file *, int, struct file_lock *); 
  20.  ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int); 
  21.  unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long); 
  22.  int (*check_flags)(int); 
  23.  int (*flock) (struct file *, int, struct file_lock *); 
  24.  ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int); 
  25.  ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int); 
  26.  int (*setlease)(struct file *, long, struct file_lock **); 
  27.  long (*fallocate)(struct file *file, int mode, loff_t offset, 
  28.      loff_t len); 
  29.  int (*show_fdinfo)(struct seq_file *m, struct file *f); 
  30. }; 

那么我们应该如何定义该结构体变量并初始化呢?

  1. static struct file_operations hello_ops =  
  2.  .open = hello_open, 
  3.  .release = hello_release, 
  4.  .read = hello_read, 
  5.  .write = hello_write, 
  6. }; 

函数定义如下:

  1. static int hello_open (struct inode *inode, struct file *filep) 
  2.  return 0; 
  3. static int hello_release (struct inode *inode, struct file *filep) 
  4.  return 0; 
  5.  
  6. static ssize_t hello_read (struct file *filep, char __user *buf, size_t size, loff_t *pos) 
  7.  return size
  8. static ssize_t hello_write (struct file *filep, const char __user *buf, size_t size, loff_t *pos) 
  9.  return size

注意,函数的参数和返回值,必须严格按照结构体struct file_operations中的类型定义。【编辑推荐】

 

责任编辑:武晓燕 来源: 一口Linux
相关推荐

2023-11-28 12:19:49

C++函数指针

2009-04-28 13:25:36

Ajax函数Java

2022-04-28 09:05:41

网络爬虫Python

2020-04-20 09:02:33

函数RPCCPU

2021-11-10 10:48:36

C++函数指针

2010-04-02 10:29:02

CentOS安装

2019-07-29 09:25:01

云优先工作负载公共云

2021-08-26 07:43:44

vectorerase错误

2021-02-20 11:34:43

Linux内核指针

2011-11-24 14:20:24

Java

2021-04-15 08:55:51

Go struc代码

2021-04-29 15:04:59

云安全云计算网络安全

2021-01-13 06:58:35

C语言函数指针

2011-07-15 01:20:58

C指针函数函数指针

2022-07-26 16:08:43

print函数

2021-12-07 06:55:17

节流函数Throttle

2015-05-25 15:06:28

JavaScript函数式编程

2012-11-19 11:07:42

IBMdw

2013-04-08 10:54:51

Javascript

2022-01-05 21:50:49

数据分析指标大数据
点赞
收藏

51CTO技术栈公众号