将结构的所有成员(相同,基本,数据类型)初始化为一个给定值 [英] Initializing all members (of the same, basic, data type) of a struct to one given value
问题描述
假设我有一个由几个字段组成的结构,所有字段都相同基本数据类型.
Let's say I have a struct made of several fields, all of the same basic data type.
例如:
struct myStruct {
float a;
float b;
float c;
float d;
float e;
float f;
}
是否存在一种聪明的方法来将所有成员初始化或设置为给定值,例如-1
或0xDEADBEEF
,其方式可以灵活地更改字段数和字段名称?
Is there a smart approach to initialize or set all members to a given value, e.g. -1
, or 0xDEADBEEF
, in a way that is flexible to changes in the number of fields and in the field names?
理论值:
将所有字段初始化为无效状态,并确保稍后在我添加新字段时初始化所有字段.
Rationale:
Initializing all fields to an invalid state, and make sure all fields are initialized if later on I add new fields.
注意:
如果有一种仅适用于整数类型的解决方案,我还是很感兴趣.
Note:
If there is a solution that would only work for integer types, I am anyway interested.
这与数组初始化和结构的零初始化,在这里,我要问的是将具有相同基本数据类型的字段的结构初始化为自定义值.
This is a different question from array initialization and zero-initialization of a struct, as here I am asking about initializing a struct, with fields all of the same basic data type, to a custom value.
此问题与结构中的数组无关,在具有初始值的C结构中也未将其处理关于所有数据字段具有相同的基本数据类型的情况
This question, which doesn't concern arrays inside a struct, is also not answered at Initialize values of array in a struct. It's also not treated in structs in C with initial values, as I am asking about the case in which all data field have the same, basic, data type
推荐答案
解决此问题的一种方法是使用X宏概念 link1 ,链接2 .
One way to address this problem is using the X Macro concept link1, link 2.
结构成员的列表将是
#define LIST_OF_STRUCT_MEMBERS \
X(a) \
X(b) \
X(c)
然后将结构声明为:
#define X(name) int name;
struct myStruct {
LIST_OF_STRUCT_MEMBERS
}
#undef X
int
可以用任何基本数据类型替换的地方.
Where int
could be replaced by any basic data type.
然后可以创建一个初始化为默认值的 const变量:
One could then create a const variable initialized to the default value:
#define X(name) -1,
const struct myStruct myStruct_DEFAULT_VALUE = { LIST_OF_STRUCT_MEMBERS };
#undef X
预处理器将其翻译为:
const struct myStruct myStruct_DEFAULT_VALUE = {-1, -1, -1,};
每次必须初始化新变量时,都可以复制.
This can be copied every time a new variable has to be initialized.
可以轻松验证该方法,以与定义打印功能相同的方式:
The method can easily be verified defining in the same way a print function:
#define X(name) printf("%s = %d\n", #name, in->name);
void printAllMembers(struct myStruct* in) {
LIST_OF_STRUCT_MEMBERS
}
#undef X
(更改基础基本数据类型时,可能需要对printAllMembers函数进行一些修改).
(The printAllMembers function might need some modification when the underlying basic data type is changed).
已验证此处.
如果我们将数据类型包含在LIST_OF_STRUCT_MEMBERS
中,则该概念可以推广到具有不同数据类型的字段的结构,例如:
This concept can generalize to a struct with fields of different data types if we include the data type in LIST_OF_STRUCT_MEMBERS
, e.g.:
#define LIST_OF_STRUCT_MEMBERS \
X(int, a) \
X(float, b) \
X(double, c)
#define X(type, name) type name;
struct myStruct {
LIST_OF_STRUCT_MEMBERS
};
#undef X
#define X(type, name) -1,
const struct myStruct myStruct_DEFAULT_VALUE = { LIST_OF_STRUCT_MEMBERS };
#undef X
#define intFormatting "%d"
#define floatFormatting "%f"
#define doubleFormatting "%f"
#define X(type, name) printf(#name " = " type ## Formatting "\n", in->name);
void printMyStruct (struct myStruct* in) {
LIST_OF_STRUCT_MEMBERS
}
#undef X
在此处进行了测试.
这篇关于将结构的所有成员(相同,基本,数据类型)初始化为一个给定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!