问题描述
假设我有一个由多个字段组成的结构,所有字段都相同基本数据类型.
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 变量,初始化为默认值:
#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
", #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 "
", in->name);
void printMyStruct (struct myStruct* in) {
LIST_OF_STRUCT_MEMBERS
}
#undef X
测试这里.
这篇关于将结构的所有成员(具有相同的基本数据类型)初始化为一个给定值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!