C语言 C中结构成员的默认值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13716913/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
default value for struct member in C
提问by user1508893
Is it possible to set default values for some struct member? I tried the following but, it'd cause syntax error:
是否可以为某些结构成员设置默认值?我尝试了以下操作,但是会导致语法错误:
typedef struct
{
int flag = 3;
} MyStruct;
Errors:
错误:
$ gcc -o testIt test.c
test.c:7: error: expected ‘:', ‘,', ‘;', ‘}' or ‘__attribute__' before ‘=' token
test.c: In function ‘main':
test.c:17: error: ‘struct <anonymous>' has no member named ‘flag'
回答by Alok Save
Structure is a data type. You don't give values to a data type. You give values to instances/objects of data types.
So no this is not possible in C.
结构是一种数据类型。您不会为数据类型提供值。您为数据类型的实例/对象赋予值。
所以不,这在 C 中是不可能的。
Instead you can write a function which does the initialization for structure instance.
相反,您可以编写一个函数来对结构实例进行初始化。
Alternatively, You could do:
或者,你可以这样做:
struct MyStruct_s
{
int id;
} MyStruct_default = {3};
typedef struct MyStruct_s MyStruct;
And then always initialize your new instances as:
然后始终将您的新实例初始化为:
MyStruct mInstance = MyStruct_default;
回答by Chirag Desai
I agree with Als that you can not initialize at time of defining the structure in C. But you can initialize the structure at time of creating instance shown as below.
我同意 Als 的观点,你不能在 C 中定义结构时初始化。但是你可以在创建实例时初始化结构,如下所示。
In C,
在 C 中,
struct s {
int i;
int j;
};
struct s s_instance = { 10 ,20 };
in C++ its possible to give direct value in definition of structure shown as below
在 C++ 中,可以在如下所示的结构定义中给出直接值
struct s {
int i;
s(): i(10)
{
}
};
回答by David Callanan
Create a default struct as the other answers have mentioned:
正如其他答案所提到的那样,创建一个默认结构:
struct MyStruct
{
int flag;
}
MyStruct_default = {3};
However, the above code will not work in a header file - you will get error: multiple definition of 'MyStruct_default'. To solve this problem, use externinstead in the header file:
但是,上面的代码在头文件中不起作用 - 你会得到错误:multiple definition of 'MyStruct_default'. 要解决此问题,请extern在头文件中使用:
struct MyStruct
{
int flag;
};
extern const struct MyStruct MyStruct_default;
And in the cfile:
在c文件中:
const struct MyStruct MyStruct_default = {3};
Hope this helps anyone having trouble with the header file.
希望这可以帮助任何在头文件方面遇到问题的人。
回答by Adeel Ahmed
You can use some function to initialize struct as follows,
您可以使用一些函数来初始化结构,如下所示,
typedef struct
{
int flag;
} MyStruct;
MyStruct GetMyStruct(int value)
{
MyStruct My = {0};
My.flag = value;
return My;
}
void main (void)
{
MyStruct temp;
temp = GetMyStruct(3);
printf("%d\n", temp.flag);
}
EDIT:
编辑:
typedef struct
{
int flag;
} MyStruct;
MyStruct MyData[20];
MyStruct GetMyStruct(int value)
{
MyStruct My = {0};
My.flag = value;
return My;
}
void main (void)
{
int i;
for (i = 0; i < 20; i ++)
MyData[i] = GetMyStruct(3);
for (i = 0; i < 20; i ++)
printf("%d\n", MyData[i].flag);
}
回答by Israel Unterman
An initialization function to a struct is a good way to grant it default values:
结构体的初始化函数是授予其默认值的好方法:
Mystruct s;
Mystruct_init(&s);
Or even shorter:
或者更短:
Mystruct s = Mystruct_init(); // this time init returns a struct
回答by Agriculturist
Another approach to default values. Make an initialization function with the same type as the struct. This approach is very useful when splitting large code into separate files.
默认值的另一种方法。使用与结构相同的类型创建一个初始化函数。这种方法在将大代码拆分为单独的文件时非常有用。
struct structType{
int flag;
};
struct structType InitializeMyStruct(){
struct structType structInitialized;
structInitialized.flag = 3;
return(structInitialized);
};
int main(){
struct structType MyStruct = InitializeMyStruct();
};
回答by CSharpino
Another approach, if the struct allows it, is to use a #define with the default values inside:
如果结构允许,另一种方法是使用带有默认值的#define:
#define MYSTRUCT_INIT { 0, 0, true }
typedef struct
{
int id;
int flag;
bool foo;
} MyStruct;
Use:
用:
MyStruct val = MYSTRUCT_INIT;
回答by Shibu J
I think the following way you can do it,
我认为您可以通过以下方式做到这一点,
typedef struct
{
int flag : 3;
} MyStruct;

