【发布时间】:2021-07-22 09:07:46
【问题描述】:
我想将我的结构的每个元素(或其他东西,可能无法使用结构)与递增索引相关联,有点像枚举,但与值字段相关联。 假设我有这个数据结构:
typedef struct
{
int8_t value_at_index0; // start with index 0 for this field
int8_t value_at_index1; // then index = 1, etc
int8_t value_at_index2;
int8_t value_at_index3;
int8_t value_at_index4;
} data;
我想获取一个字段的索引,索引应该反映该字段在结构中声明的位置。
编辑:我的问题是我想在外部存储器中写入值,但我不想确定哪个索引是值。例如我想这样做:write(index.value_at_index2, data.value_at_index2)
谢谢
【问题讨论】:
-
associate each element of my struct (or something else, may not be possible with a struct) with an incrementing index有什么用?您将如何使用该索引?For now I'm simply declaring请发布您的代码。你为什么这样做?为什么要“在 for 循环中声明两个相同的结构”,而您“增加索引”是为了什么? -
@KamilCuk:用于在内存中写入数据。我有一个结构来映射内存的值
-
只需将整个结构写入内存
memcpy(memory, &data, sizeof(data))。 -
这听起来很像XY Problem。你实际上想要达到什么目的?
-
如果我只想将结构的一个元素写入我的记忆?例如
write(index, data.value_at_index2),如何获取索引(假设我不知道)?