sud*_*03r 2 c struct initialization
在阅读我遇到的代码时,结构的以下定义和初始化:
// header file
struct foo{
char* name;
int value;
};
//Implementation file
struct foo fooElmnt __foo;
// some code using fooElmnt
struct foo fooElmnt __foo = {
.name = "NAME";
.value = some_value;
}
Run Code Online (Sandbox Code Playgroud)
这在C中意味着什么?它与通常的声明有什么不同?
它被称为指定初始化,
在结构初始值设定项中,指定要
.fieldname =
在元素值之前初始化的字段的名称 .例如,给定以下结构,Run Code Online (Sandbox Code Playgroud)struct point { int x, y; };
以下初始化
Run Code Online (Sandbox Code Playgroud)struct point p = { .y = yvalue, .x = xvalue };
相当于
Run Code Online (Sandbox Code Playgroud)struct point p = { xvalue, yvalue };
如果您继续阅读,它会解释.fieldname
为被称为指示符.
更新:我不是C99专家,但我无法编译代码.这是我必须做出的改变:
// header file
struct foo{
char* name;
int value;
};
//Implementation file
//struct foo fooElmnt __foo;
// some code using fooElmnt
struct foo fooElmnt = {
.name = "NAME",
.value = 123
};
Run Code Online (Sandbox Code Playgroud)
你能编译吗?我用过TCC.