成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

函數指針定義的一個錯誤

開發 前端
字符設備,內核給多有的字符設備提供了一個統一的接口,我們對設備的所有操作被抽象成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++函數指針

2022-04-28 09:05:41

網絡爬蟲Python

2009-04-28 13:25:36

Ajax函數Java

2020-04-20 09:02:33

函數RPCCPU

2021-11-10 10:48:36

C++函數指針

2010-04-02 10:29:02

CentOS安裝

2025-06-17 08:10:00

智能指針C++代碼

2019-07-29 09:25:01

云優先工作負載公共云

2021-08-26 07:43:44

vectorerase錯誤

2011-11-24 14:20:24

Java

2021-04-15 08:55:51

Go struc代碼

2021-04-29 15:04:59

云安全云計算網絡安全

2021-02-20 11:34:43

Linux內核指針

2021-01-13 06:58:35

C語言函數指針

2015-05-25 15:06:28

JavaScript函數式編程

2012-08-23 14:23:33

函數式編程

2022-07-26 16:08:43

print函數

2021-12-07 06:55:17

節流函數Throttle

2022-10-13 09:43:31

MySQL索引聯合索引

2021-01-28 07:38:29

C指針C語言應用程序
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 欧美激情综合网 | 日韩久久久久久 | 中文字幕高清 | 日韩一区二区三区在线观看 | 亚洲成人精品久久 | 国产在线第一页 | 久久综合一区 | 91精品国产一区二区 | 久久久久久亚洲欧洲 | 2018国产大陆天天弄 | 国产精品99久久久久久动医院 | 99精品视频一区二区三区 | 日韩av成人 | www.成人久久 | 都市激情亚洲 | 国内精品久久久久 | 黄色免费在线网址 | 欧美日韩国产三级 | 一区二区三区国产好的精 | av一区二区三区在线观看 | 91不卡在线 | 精品国产一区二区三区久久狼黑人 | 久久久久久影院 | 日一区二区 | 一区二区久久电影 | 亚洲国产精品久久 | 欧美精品日韩 | 狠狠狠 | 国产成人福利视频在线观看 | 国产极品车模吞精高潮呻吟 | 国产精品视频久久久久久 | 亚洲美女在线视频 | 7777在线视频免费播放 | 中文字幕亚洲欧美 | 久久国产成人精品国产成人亚洲 | 久久综合九色综合欧美狠狠 | 中文字幕久久精品 | 欧美日韩在线一区二区三区 | 欧美婷婷 | 亚洲精品v日韩精品 | 99久久国产综合精品麻豆 |