函數(shù)指針編譯時(shí)出錯(cuò)怎么辦?
2) 分步解析
有的C語(yǔ)言基礎(chǔ)不是很好的朋友,可能無(wú)法一眼看出來(lái)這個(gè)定義,為了讓新手更容易看懂,我們來(lái)看一下下面一個(gè)遞進(jìn)式的定義:
int fun;
這是一個(gè)整型變量fun;
int fun();
這是一個(gè)函數(shù)fun,參數(shù) :空返回值:int型
int fun(struct touch_message *);
這是一個(gè)函數(shù)fun,參數(shù) :struct touch_message *的一個(gè)指針?lè)祷刂担篿nt型
上述的變化都好理解,下面我們將fun做如下修改:
int (*fun)(struct touch_message *);
括號(hào)的優(yōu)先級(jí)最高,(fun)一旦如此定義,那么fun就要先和結(jié)合,所以fun變成了一個(gè)指針,
那么該指針指向什么呢?就需要看外面是如何定義的,右邊是(struct touch_message * ),左邊是int,所以說(shuō)明指針指向的是一個(gè)函數(shù),
參數(shù) :struct touch_message *的一個(gè)指針?lè)祷刂担篿nt型
舉例:將函數(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ù)類(lèi)型必須和函數(shù)指針類(lèi)型一致。
typedef int (*fun)(struct touch_message *);
如果左邊再加上typedef呢?相當(dāng)于是設(shè)置fun為新的類(lèi)型,我們可以用fun來(lái)定義一個(gè)函數(shù)指針,該函數(shù)類(lèi)型同上。
舉例用新的類(lèi)型定義一個(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,就得到了粉絲的源碼中的樣子,
typedef int (*fun)(touch_message_t);
但是粉絲的源碼中定義的函數(shù)類(lèi)型缺少了對(duì)函數(shù)返回值的描述,所以左側(cè)增加一個(gè)int或者其他類(lèi)型即可即可。
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 中:
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);
};
那么我們應(yīng)該如何定義該結(jié)構(gòu)體變量并初始化呢?
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ù)和返回值,必須嚴(yán)格按照結(jié)構(gòu)體struct file_operations中的類(lèi)型定義。

發(fā)表評(píng)論
請(qǐng)輸入評(píng)論內(nèi)容...
請(qǐng)輸入評(píng)論/評(píng)論長(zhǎng)度6~500個(gè)字
圖片新聞
-
機(jī)器人奧運(yùn)會(huì)戰(zhàn)報(bào):宇樹(shù)機(jī)器人摘下首金,天工Ultra搶走首位“百米飛人”
-
存儲(chǔ)圈掐架!江波龍起訴佰維,索賠121萬(wàn)
-
長(zhǎng)安汽車(chē)母公司突然更名:從“中國(guó)長(zhǎng)安”到“辰致科技”
-
豆包前負(fù)責(zé)人喬木出軌BP后續(xù):均被辭退
-
字節(jié)AI Lab負(fù)責(zé)人李航卸任后返聘,Seed進(jìn)入調(diào)整期
-
員工持股爆雷?廣汽埃安緊急回應(yīng)
-
中國(guó)“智造”背后的「關(guān)鍵力量」
-
小米汽車(chē)研發(fā)中心重磅落地,寶馬家門(mén)口“搶人”
最新活動(dòng)更多
-
10月23日火熱報(bào)名中>> 2025是德科技創(chuàng)新技術(shù)峰會(huì)
-
10月23日立即報(bào)名>> Works With 開(kāi)發(fā)者大會(huì)深圳站
-
10月24日立即參評(píng)>> 【評(píng)選】維科杯·OFweek 2025(第十屆)物聯(lián)網(wǎng)行業(yè)年度評(píng)選
-
11月27日立即報(bào)名>> 【工程師系列】汽車(chē)電子技術(shù)在線大會(huì)
-
12月18日立即報(bào)名>> 【線下會(huì)議】OFweek 2025(第十屆)物聯(lián)網(wǎng)產(chǎn)業(yè)大會(huì)
-
精彩回顧立即查看>> 【限時(shí)福利】TE 2025國(guó)際物聯(lián)網(wǎng)展·深圳站
推薦專(zhuān)題
- 1 人形機(jī)器人,正狂奔在批量交付的曠野
- 2 宇樹(shù)機(jī)器人撞人事件的深度剖析:六維力傳感器如何成為人機(jī)安全的關(guān)鍵屏障
- 3 解碼特斯拉新AI芯片戰(zhàn)略 :從Dojo到AI5和AI6推理引擎
- 4 AI版“四萬(wàn)億刺激”計(jì)劃來(lái)了
- 5 2025年8月人工智能投融資觀察
- 6 7 a16z最新AI百?gòu)?qiáng)榜:硅谷頂級(jí)VC帶你讀懂全球生成式AI賽道最新趨勢(shì)
- 8 一家被嚴(yán)重低估的國(guó)產(chǎn)AI巨頭
- 9 Manus跑路,大廠掉線,只能靠DeepSeek了
- 10 地平線的野心:1000萬(wàn)套HSD上車(chē)