最近在学习linux中,发现内核源码中出现很多结构体元素前面加一点(.),后来查看资料发现原来就是给元素赋值的意思,话不多说,直接上代码
1、一般结构体赋值
struct student{
int a; //此处是分号
int b;
};
struct stdent stu;
stu.a = 10;
stu.b = 20;
2、直接赋值
struct student stu{
.a = 10, //此处是逗号
.b = 20,
};
3、linux内核源码体现
static struct file_operations test_fops = {
.owner = THIS_MODULE,
.open = chrtest_open,
.read = chrtest_read,
.write = chrtest_write,
.release = chrtest_release,
};
通过上述对比,前面加一点就是正常的赋值操作,只不过写法让读者有点陌生
加一点另外的用处就是可以改变赋值的优先级,加一点就必须是先给owner赋值在给open赋值。
欢迎读者提出文章有误的地方,感谢!!!