Linux 中C语言的一点高级用法

在LINUX中内核中有很多C语言的奇技淫巧,比如container_of()这个宏, 在很多地方会用到,比如在定义宏to_i2c_driver, to_spi_driver时都用到这个,有些就直接在函数中调用 container_of宏。

1. 作用

这个宏的作用就是通过一个结构体的某个成员的指针,反推出这个结构体的首地址。
这种操作非常有用,尤其在C语言中写面向对象的程序时,相当模拟了C++多态的效果。以LINUX内核中I2C子系统为例, i2c驱动结构体如下:

struct i2c_driver {
    unsigned int class;

    /* Standard driver model interfaces */
    int (*probe)(struct i2c_client *client, const struct i2c_device_id *id);
    void (*remove)(struct i2c_client *client);

    /* New driver model interface to aid the seamless removal of the
     * current probe()'s, more commonly unused than used second parameter.
     */
    int (*probe_new)(struct i2c_client *client);

    /* driver model interfaces that don't relate to enumeration  */
    void (*shutdown)(struct i2c_client *client);

    /* Alert callback, for example for the SMBus alert protocol.
     * The format and meaning of the data value depends on the protocol.
     * For the SMBus alert protocol, there is a single bit of data passed
     * as the alert response's low bit ("event flag").
     * For the SMBus Host Notify protocol, the data corresponds to the
     * 16-bit payload data reported by the slave device acting as master.
     */
    void (*alert)(struct i2c_client *client, enum i2c_alert_protocol protocol,
              unsigned int data);

    /* a ioctl like command that can be used to perform specific functions
     * with the device.
     */
    int (*command)(struct i2c_client *client, unsigned int cmd, void *arg);

    struct device_driver driver;
    const struct i2c_device_id *id_table;

    /* Device detection callback for automatic device creation */
    int (*detect)(struct i2c_client *client, struct i2c_board_info *info);
    const unsigned short *address_list;
    struct list_head clients;

    u32 flags;
};

在struct i2c_driver 中有一个成员struct device_driver driver, 这相当于“is-a”的关系,即 i2c_driver 是一种 device_driver. 这种操作实际就是继承。

紧接着定义了一个i2c

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Linux下使用C语言封装SQLite可以通过SQLite提供的C API来实现。SQLite是一个轻量级、嵌入式的数据库引擎,适用于移动设备和单用户应用程序。下面是使用C语言封装SQLite的基本步骤: 1. 安装SQLite库:在Linux上安装SQLite库可以通过包管理器(如apt、yum)进行安装,或者从SQLite官方网站下载源代码进行编译安装。 2. 包含SQLite头文件:在C语言代码包含SQLite的头文件,例如:`#include <sqlite3.h>` 3. 打开数据库连接:使用`sqlite3_open()`函数打开与数据库的连接,并获取一个`sqlite3`结构体的指针。示例代码如下: ```c sqlite3 *db; int rc = sqlite3_open("database.db", &db); if (rc != SQLITE_OK) { // 处理连接失败的情况 } ``` 4. 执行SQL语句:可以使用`sqlite3_exec()`函数执行SQL语句,该函数可以处理任意类型的SQL语句,包括查询、插入、更新和删除等。示例代码如下: ```c const char* sql = "CREATE TABLE IF NOT EXISTS students (id INTEGER PRIMARY KEY, name TEXT, age INT)"; rc = sqlite3_exec(db, sql, 0, 0, 0); if (rc != SQLITE_OK) { // 处理SQL语句执行失败的情况 } ``` 5. 处理查询结果:可以使用`sqlite3_exec()`函数的第三个参数来指定一个回调函数,在查询时调用该函数处理查询结果。示例代码如下: ```c int callback(void* data, int argc, char** argv, char** column_names) { for (int i = 0; i < argc; ++i) { printf("%s = %s\n", column_names[i], argv[i] ? argv[i] : "NULL"); } printf("\n"); return 0; } const char* sql = "SELECT * FROM students"; rc = sqlite3_exec(db, sql, callback, 0, 0); if (rc != SQLITE_OK) { // 处理SQL语句执行失败的情况 } ``` 6. 关闭数据库连接:使用`sqlite3_close()`函数关闭与数据库的连接。示例代码如下: ```c sqlite3_close(db); ``` 通过上述步骤,可以在Linux下使用C语言封装SQLite,实现对数据库的增删改查等操作。当然,这只是SQLite的基本使用方法,还有更多高级的特性和操作可以进一步探索。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值