函數(shù)指針編譯時出錯怎么辦?
2) 分步解析
有的C語言基礎不是很好的朋友,可能無法一眼看出來這個定義,為了讓新手更容易看懂,我們來看一下下面一個遞進式的定義:
int fun;
這是一個整型變量fun;
int fun();
這是一個函數(shù)fun,參數(shù) :空返回值:int型
int fun(struct touch_message *);
這是一個函數(shù)fun,參數(shù) :struct touch_message *的一個指針返回值:int型
上述的變化都好理解,下面我們將fun做如下修改:
int (*fun)(struct touch_message *);
括號的優(yōu)先級最高,(fun)一旦如此定義,那么fun就要先和結合,所以fun變成了一個指針,
那么該指針指向什么呢?就需要看外面是如何定義的,右邊是(struct touch_message * ),左邊是int,所以說明指針指向的是一個函數(shù),
參數(shù) :struct touch_message *的一個指針返回值:int型
舉例:將函數(shù)my_fun賦值給函數(shù)指針fun。int my_fun(struct touch_message *){}int (*fun)(struct touch_message *);fun = my_fun;
這里有一個隱藏的知識點,函數(shù)名其實也是一個地址,而且賦值的時候函數(shù)類型必須和函數(shù)指針類型一致。
typedef int (*fun)(struct touch_message *);
如果左邊再加上typedef呢?相當于是設置fun為新的類型,我們可以用fun來定義一個函數(shù)指針,該函數(shù)類型同上。
舉例用新的類型定義一個函數(shù)指針變量,并給他賦值。typedef int (*fun)(struct touch_message *);int my_fun(struct touch_message *){}fun fun_ptr;fun_ptr = my_fun;
然后將參數(shù)修改為,touch_message_t,就得到了粉絲的源碼中的樣子,
typedef int (*fun)(touch_message_t);
但是粉絲的源碼中定義的函數(shù)類型缺少了對函數(shù)返回值的描述,所以左側增加一個int或者其他類型即可即可。
3. 函數(shù)指針
函數(shù)指針在linux內核中使用非常頻繁,
比如字符設備,內核給多有的字符設備提供了一個統(tǒng)一的接口,我們對設備的所有操作被抽象成read、write、open、close等,并封裝到結構體struct file_operations 中:
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,
};
函數(shù)定義如下:
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;
}
注意,函數(shù)的參數(shù)和返回值,必須嚴格按照結構體struct file_operations中的類型定義。

請輸入評論內容...
請輸入評論/評論長度6~500個字
最新活動更多
-
10月23日火熱報名中>> 2025是德科技創(chuàng)新技術峰會
-
10月23日立即報名>> Works With 開發(fā)者大會深圳站
-
10月24日立即參評>> 【評選】維科杯·OFweek 2025(第十屆)物聯(lián)網行業(yè)年度評選
-
即日-11.25立即下載>>> 費斯托白皮書《柔性:汽車生產未來的關鍵》
-
11月27日立即報名>> 【工程師系列】汽車電子技術在線大會
-
12月18日立即報名>> 【線下會議】OFweek 2025(第十屆)物聯(lián)網產業(yè)大會
-
9 每日AI全球觀察
- 1 特斯拉工人被故障機器人打成重傷,索賠3.6億
- 2 【行業(yè)深度研究】退居幕后四年后,張一鳴終于把算法公司變成AI公司?
- 3 AI 時代,阿里云想當“安卓” ,那誰是“蘋果”?
- 4 拐點已至!匯川領跑工控、埃斯頓份額第一、新時達海爾賦能扭虧為盈
- 5 硬剛英偉達!華為發(fā)布全球最強算力超節(jié)點和集群
- 6 隱退4年后,張一鳴久違現(xiàn)身!互聯(lián)網大佬正集體殺回
- 7 00后華裔女生靠兩部AI電影狂賺7.8億人民幣,AI正式進軍好萊塢
- 8 谷歌“香蕉”爆火啟示:國產垂類AI的危機還是轉機?
- 9 機器人9月大事件|3家國產機器人沖刺IPO,行業(yè)交付與融資再創(chuàng)新高!
- 10 美光:AI Capex瘋投不止,終于要拉起存儲超級周期了?
- 生產部總監(jiān) 廣東省/廣州市
- 資深管理人員 廣東省/江門市
- Regional Sales Manager 廣東省/深圳市
- 銷售總監(jiān) 廣東省/深圳市
- 結構工程師 廣東省/深圳市
- 光器件研發(fā)工程師 福建省/福州市
- 自動化高級工程師 廣東省/深圳市
- 技術專家 廣東省/江門市
- 激光器高級銷售經理 上海市/虹口區(qū)
- 封裝工程師 北京市/海淀區(qū)