结构声明数组c [英] C array of structs declaration
问题描述
在Linux内核中,我看到结构的数组,看起来像这样的声明
In the Linux kernel, I see a declaration of an array of structs that looks like this
struct SomeStructName [] ={
[SOMEWEIRD_NAME] = {
.field1 = "some value"
},
[SOMEWEIRD_NAME2] = {
.field1 = "some value1"
},
}
我从来没有见过这样的声明,具体我也弄不清是什么 [SOMEWEIRD_NAME]
表示,为什么它的使用。
I have never seen a declaration like that, specifically I can't figure out what [SOMEWEIRD_NAME]
means, and why it's used.
推荐答案
这是一个C99的 指定的初始化 的数组。
It's a C99 designated initializer for arrays.
例如:
/*
* Initialize element 0 to 1
* 1 to 2
* 2 to 3
* 255 to 1
* and all other elements to 0
*/
int arr[256] = {[0] = 1, 2, 3, [255] = 1};
它允许你初始化任何顺序一些具体的数组元素,也可以让你忽略一些元素。
It allows you to initialize some specific array elements in any order and also allows you to omit some elements.
在你的榜样之间的前pression []
可以是一个整型常量前pression或宏名枚举
不变。它不能是一个变量名,因为它必须是一个整型常量前pression。
In your example the expression between []
can be a macro name for an integer constant expression or an enum
constant. It cannot be a variable name as it has to be an integer constant expression.
这篇关于结构声明数组c的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!