[英]default value for struct member in C
是否可以为某些结构成员设置默认值? 我尝试了以下操作,但是会导致语法错误:
typedef struct
{
int flag = 3;
} MyStruct;
错误:
$ 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’
结构是一种数据类型。 您不会为数据类型提供值。 您为数据类型的实例/对象赋予值。
所以不,这在 C 中是不可能的。
相反,您可以编写一个函数来对结构实例进行初始化。
或者,你可以这样做:
struct MyStruct_s
{
int id;
} MyStruct_default = {3};
typedef struct MyStruct_s MyStruct;
然后始终将您的新实例初始化为:
MyStruct mInstance = MyStruct_default;
你不能这样做
请改用以下内容
typedef struct
{
int id;
char* name;
}employee;
employee emp = {
.id = 0,
.name = "none"
};
您可以使用宏来定义和初始化您的实例。 每次你想定义新实例并初始化它时,这会让你更容易。
typedef struct
{
int id;
char* name;
}employee;
#define INIT_EMPLOYEE(X) employee X = {.id = 0, .name ="none"}
在您的代码中,当您需要使用员工类型定义新实例时,您只需调用这个宏,如:
INIT_EMPLOYEE(emp);
我同意 Als 的观点,你不能在 C 中定义结构时初始化。但是你可以在创建实例时初始化结构,如下所示。
在 C 中,
struct s {
int i;
int j;
};
struct s s_instance = { 10 ,20 };
在 C++ 中,可以在如下所示的结构定义中给出直接值
struct s {
int i;
s(): i(10)
{
}
};
你可以做:
struct employee_s {
int id;
char* name;
} employee_default = {0, "none"};
typedef struct employee_s employee;
然后你只需要记住在你声明一个新的员工变量时做默认的初始化:
employee foo = employee_default;
或者,您可以始终通过工厂函数构建您的员工结构。
如果您正在使用gcc
您可以在对象创建中提供designated initializers
值设定项。
typedef struct
{
int id=0;
char* name="none";
}employee;
employee e =
{
.id = 0;
.name = "none";
};
或者,简单地使用像数组初始化。
employee e = {0 , "none"};
正如其他答案所提到的那样,创建一个默认结构:
struct MyStruct
{
int flag;
}
MyStruct_default = {3};
但是,上面的代码在头文件中不起作用 - 你会得到错误: multiple definition of 'MyStruct_default'
。 要解决此问题,请在头文件中使用extern
代替:
struct MyStruct
{
int flag;
};
extern const struct MyStruct MyStruct_default;
在c
文件中:
const struct MyStruct MyStruct_default = {3};
希望这可以帮助任何在头文件方面遇到问题的人。
更重要的是,要添加现有答案,您可以使用隐藏结构初始值设定项的宏:
#define DEFAULT_EMPLOYEE { 0, "none" }
然后在你的代码中:
employee john = DEFAULT_EMPLOYEE;
您可以实现一个初始化函数:
employee init_employee() {
empolyee const e = {0,"none"};
return e;
}
您可以使用一些函数来初始化结构,如下所示,
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);
}
编辑:
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);
}
您可以将C 预处理器函数与 varargs 、复合字面量和指定的初始值设定项结合使用, 以获得最大的便利:
typedef struct {
int id;
char* name;
} employee;
#define EMPLOYEE(...) ((employee) { .id = 0, .name = "none", ##__VA_ARGS__ })
employee john = EMPLOYEE(.name="John"); // no id initialization
employee jane = EMPLOYEE(.id=5); // no name initialization
如果你只使用这个结构一次,即创建一个全局/静态变量,你可以删除typedef
,并立即初始化这个变量:
struct {
int id;
char *name;
} employee = {
.id = 0,
.name = "none"
};
然后,您可以在代码中使用employee
。
结构体的初始化函数是授予其默认值的好方法:
Mystruct s;
Mystruct_init(&s);
或者更短:
Mystruct s = Mystruct_init(); // this time init returns a struct
默认值的另一种方法。 使用与结构相同的类型创建一个初始化函数。 这种方法在将大代码拆分为单独的文件时非常有用。
struct structType{
int flag;
};
struct structType InitializeMyStruct(){
struct structType structInitialized;
structInitialized.flag = 3;
return(structInitialized);
};
int main(){
struct structType MyStruct = InitializeMyStruct();
};
您可以为其创建一个函数:
typedef struct {
int id;
char name;
} employee;
void set_iv(employee *em);
int main(){
employee em0; set_iv(&em0);
}
void set_iv(employee *em){
(*em).id = 0;
(*em).name = "none";
}
如果结构允许,另一种方法是使用带有默认值的#define:
#define MYSTRUCT_INIT { 0, 0, true }
typedef struct
{
int id;
int flag;
bool foo;
} MyStruct;
采用:
MyStruct val = MYSTRUCT_INIT;
我认为您可以通过以下方式做到这一点,
typedef struct
{
int flag : 3;
} MyStruct;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.