Is it possible to set default values for some struct member? I tried the following but, it'd cause syntax error:
是否可以为某个struct成员设置默认值?我试过以下方法,但会导致语法错误:
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’
6 个解决方案
#1
69
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;
#2
10
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)
{
}
};
#3
2
You can use some function to initialize struct as follows,
可以使用一些函数初始化struct,如下所示,
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);
}
#4
0
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
#5
0
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();
};
#6
0
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 extern
instead in the header file:
但是,上面的代码不能在头文件中工作—您将会得到错误:“MyStruct_default”的多个定义。要解决这个问题,请在头文件中使用extern:
struct MyStruct
{
int flag;
};
extern const struct MyStruct MyStruct_default;
And in the c
file:
在c文件中:
const struct MyStruct MyStruct_default = {3};
Hope this helps anyone having trouble with the header file.
希望这能帮助任何有问题的头文件。
#1
69
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;
#2
10
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)
{
}
};
#3
2
You can use some function to initialize struct as follows,
可以使用一些函数初始化struct,如下所示,
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);
}
#4
0
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
#5
0
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();
};
#6
0
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 extern
instead in the header file:
但是,上面的代码不能在头文件中工作—您将会得到错误:“MyStruct_default”的多个定义。要解决这个问题,请在头文件中使用extern:
struct MyStruct
{
int flag;
};
extern const struct MyStruct MyStruct_default;
And in the c
file:
在c文件中:
const struct MyStruct MyStruct_default = {3};
Hope this helps anyone having trouble with the header file.
希望这能帮助任何有问题的头文件。