日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > linux >内容正文

linux

linux c语言内核函数,2014-1-5_linux内核学习(1)_C语言基础

發布時間:2023/12/20 linux 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 linux c语言内核函数,2014-1-5_linux内核学习(1)_C语言基础 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1、結構體的初始化

static struct file_operations fops = {

.read = device_read,

.write = device_write,

.open = device_open,

.release = device_release

};

以前學習C語言的時候沒有見過struct的這種初始化方式。這其實是C語言的新標準。Struct一共有三種初始化的方式:

int main(void){

struct sct{

int age;

char *name;

};

// way 1

struct sct s1={1,"aa"};

// way 2

struct sct s2={

.age=1,

.name="bb"

};

//way 3

struct sct s3={

age:1,

name:"cc"

};

printf("%d,%s\n",s1.age,s1.name);

printf("%d,%s\n",s3.age,s3.name);

printf("%d,%s\n",s2.age,s2.name);

return 0;

}

2、函數指針

static struct file_operations fops = {

.read = device_read,

.write = device_write,

.open = device_open,

.release = device_release

};

file_operations 結構體初始化傳入的4個變量是函數名,而不是普通的變量。這就涉及到指向函數的指針相關知識點。指針本身也是一個變量,作為變量本身,其會占用內存空間,其內存空間同樣可以存儲數據,并且可以讀取該數據。而作為引用使用時,即*p時,首先是需要讀取其地址存儲的數據,將其視為一個地址,再從這個地址中讀取數據,作為結果。(http://blog.chinaunix.net/uid-23629988-id-3084750.html)

簡單模擬函數指針的用法:

#include

struct file_operations{

void (*open)();

void (*close)();

};

void file_open(){

printf("file is open\n");

}

void file_close(){

printf("file is close\n");

}

void register_dev(struct file_operations *fops){

fops->open();

fops->close();

}

int main(void){

struct file_operations fops={

.open=file_open,

.close=file_close

};

register_dev(&fops);

return 0;

}

總結

以上是生活随笔為你收集整理的linux c语言内核函数,2014-1-5_linux内核学习(1)_C语言基础的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。