日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營(yíng)銷解決方案
函數(shù)指針定義的一個(gè)錯(cuò)誤

1. 問題

粉絲提問:某個(gè)函數(shù)指針的使用:編譯時(shí)出錯(cuò)了。

成都創(chuàng)新互聯(lián)公司是一家以網(wǎng)絡(luò)技術(shù)公司,為中小企業(yè)提供網(wǎng)站維護(hù)、成都網(wǎng)站設(shè)計(jì)、成都網(wǎng)站制作、網(wǎng)站備案、服務(wù)器租用、申請(qǐng)域名、軟件開發(fā)、成都微信小程序等企業(yè)互聯(lián)網(wǎng)相關(guān)業(yè)務(wù),是一家有著豐富的互聯(lián)網(wǎng)運(yùn)營(yíng)推廣經(jīng)驗(yàn)的科技公司,有著多年的網(wǎng)站建站經(jīng)驗(yàn),致力于幫助中小企業(yè)在互聯(lián)網(wǎng)讓打出自已的品牌和口碑,讓企業(yè)在互聯(lián)網(wǎng)上打開一個(gè)面向全國(guó)乃至全球的業(yè)務(wù)窗口:建站歡迎來電:13518219792

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

粉絲源碼如下:

2. 分析

1) 結(jié)構(gòu)解析

 
 
 
 
  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行這個(gè)類型定義:

 
 
 
 
  1. typedef struct touch_message * touch_message_t;

定義后

 
 
 
 
  1. touch_message_t 

等價(jià)于

 
 
 
 
  1. struct touch_message *

就是說我們?nèi)绻胻ouch_message_t 定義的變量是一個(gè)struct touch_message類型的一個(gè)指針。

再來分析下8行這個(gè)定義:

 
 
 
 
  1. typedef (*on_touch_messgae_handle)(touch_message_t);

可以替換成下面這個(gè)定義

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

2) 分步解析

有的C語(yǔ)言基礎(chǔ)不是很好的朋友,可能無法一眼看出來這個(gè)定義, 為了讓新手更容易看懂,我們來看一下下面一個(gè)遞進(jìn)式的定義:

 
 
 
 
  1. int fun;

這是一個(gè)整型變量fun;

 
 
 
 
  1. int fun();

這是一個(gè)函數(shù)fun, 參數(shù) :空 返回值:int型

 
 
 
 
  1. int fun(struct touch_message *);

這是一個(gè)函數(shù)fun, 參數(shù) :struct touch_message *的一個(gè)指針 返回值:int型

上述的變化都好理解,下面我們將fun做如下修改:

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

括號(hào)的優(yōu)先級(jí)最高,(fun)一旦如此定義,那么fun就要先和結(jié)合, 所以fun變成了一個(gè)指針,

那么該指針指向什么呢?就需要看外面是如何定義的, 右邊是(struct touch_message * ),左邊是int, 所以說明指針指向的是一個(gè)函數(shù),

參數(shù) :struct touch_message *的一個(gè)指針 返回值:int型

舉例:將函數(shù)my_fun賦值給函數(shù)指針fun。int my_fun(struct touch_message *) { } int (*fun)(struct touch_message *); fun = my_fun;

這里有一個(gè)隱藏的知識(shí)點(diǎn),函數(shù)名其實(shí)也是一個(gè)地址,而且賦值的時(shí)候函數(shù)類型必須和函數(shù)指針類型一致。

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

如果左邊再加上typedef呢?相當(dāng)于是設(shè)置fun為新的類型,我們可以用fun來定義一個(gè)函數(shù)指針, 該函數(shù)類型同上。

舉例 用新的類型定義一個(gè)函數(shù)指針變量,并給他賦值。typedef int (*fun)(struct touch_message *); int my_fun(struct touch_message *) { } fun fun_ptr; fun_ptr = my_fun;

然后將參數(shù)修改為,touch_message_t,就得到了粉絲的源碼中的樣子,

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

但是粉絲的源碼中定義的函數(shù)類型缺少了對(duì)函數(shù)返回值的描述,所以左側(cè)增加一個(gè)int或者其他類型即可即可。

3. 函數(shù)指針

函數(shù)指針在linux內(nèi)核中使用非常頻繁,

比如字符設(shè)備,內(nèi)核給多有的字符設(shè)備提供了一個(gè)統(tǒng)一的接口,我們對(duì)設(shè)備的所有操作被抽象成read、write、open、close等,并封裝到結(jié)構(gòu)體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. };

那么我們應(yīng)該如何定義該結(jié)構(gòu)體變量并初始化呢?

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

函數(shù)定義如下:

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

注意,函數(shù)的參數(shù)和返回值,必須嚴(yán)格按照結(jié)構(gòu)體struct file_operations中的類型定義。


文章標(biāo)題:函數(shù)指針定義的一個(gè)錯(cuò)誤
瀏覽地址:http://m.5511xx.com/article/cdesgjg.html