C 声明与结构和结构大小相关的数组
我在这样的条件下工作:C 声明与结构和结构大小相关的数组,c,arrays,struct,C,Arrays,Struct,我在这样的条件下工作: typedef struct __type_x{ sub_type_a a; sub_type_b b; sub_type_c c; }type_x_t; uint32_t type_x_uids[] = { 1, //a's_uid 2, //b's_uid 3, //c's_uid }; uint32_t type_x_uid_another[] = { 3, //a's uid in another API sys
typedef struct __type_x{
sub_type_a a;
sub_type_b b;
sub_type_c c;
}type_x_t;
uint32_t type_x_uids[] = {
1, //a's_uid
2, //b's_uid
3, //c's_uid
};
uint32_t type_x_uid_another[] = {
3, //a's uid in another API system
1, //b's uid in another API system
2, //c's uid in another API system
};
uint32_t type_x_uids_one_more[] = {
1, //a's_uid in yet another system
3, //b's_uid in yet another system
2, //c's_uid in yet another system
};
#include <stdint.h>
#define TYPE_X_LIST \
X(a,1,3,1) \
X(b,2,1,3) \
X(c,3,2,2)
typedef struct
{
#define X(name, dummmy1, dummy2, dummy3) sub_type_##name name;
TYPE_X_LIST
#undef X
} type_x_t;
uint32_t type_x_uids[] = {
#define X(dummy1, id, dummy2, dummy3) id,
TYPE_X_LIST
#undef X
};
uint32_t type_x_uid_another[] = {
#define X(dummy1, dummy2, id, dummy3) id,
TYPE_X_LIST
#undef X
};
uint32_t type_x_uids_one_more[] = {
#define X(dummy1, dummy2, dummy3, id) id,
TYPE_X_LIST
#undef X
};
问题是:如果我需要在structtype\u x\u t
中再添加一个数据(如d),我需要将其UID添加到所有其他三个数组中
很难维护代码
所以我想知道是否有可能将所有这些数据从四个位置保存到一个表中
我可以将三个uint32阵列放入一个2D阵列中
uint32_t uid[system_id][value];
但我还必须维护两段代码,而不是一段 我想知道我是否还有什么可以继续前进的地方?因此,我可以维护一个表来管理所有这些数据。诸如此类
{#data_type, #data_name, #uid_1, #uid2, #uid 3},
试图使用宏来解决这个问题,但我无法通过
\uu VA\u ARGS\uuu>访问特定位置的数据。首先,如果您将代码相邻地放置在一个文件中,那么实际上是在维护一个位置,而不是多个位置。你所拥有的并不一定是坏的。无论如何
我假设type\ux\t
是结构而不是数组是有原因的。显然,最好的设计是将“a”的“uid”添加到sub_type_a
struct中,或者创建一个包含这两者的新结构,因为这些数据属于同一类
如果这不是一个选项,您可以使用数组,然后将其放入type_x_t
结构中
如果这不是一个选项,考虑一些完全不同的程序设计。< /P>
随着所有程序设计选项的用尽,然后-只有到那时,你可以考虑宏。您想要的是所谓的“X宏”,这是最后的手段,并不推荐使用,因为它们使代码更难阅读。X宏的目的是将数据的代码维护集中到一个地方。事情是这样的:
typedef struct __type_x{
sub_type_a a;
sub_type_b b;
sub_type_c c;
}type_x_t;
uint32_t type_x_uids[] = {
1, //a's_uid
2, //b's_uid
3, //c's_uid
};
uint32_t type_x_uid_another[] = {
3, //a's uid in another API system
1, //b's uid in another API system
2, //c's uid in another API system
};
uint32_t type_x_uids_one_more[] = {
1, //a's_uid in yet another system
3, //b's_uid in yet another system
2, //c's_uid in yet another system
};
#include <stdint.h>
#define TYPE_X_LIST \
X(a,1,3,1) \
X(b,2,1,3) \
X(c,3,2,2)
typedef struct
{
#define X(name, dummmy1, dummy2, dummy3) sub_type_##name name;
TYPE_X_LIST
#undef X
} type_x_t;
uint32_t type_x_uids[] = {
#define X(dummy1, id, dummy2, dummy3) id,
TYPE_X_LIST
#undef X
};
uint32_t type_x_uid_another[] = {
#define X(dummy1, dummy2, id, dummy3) id,
TYPE_X_LIST
#undef X
};
uint32_t type_x_uids_one_more[] = {
#define X(dummy1, dummy2, dummy3, id) id,
TYPE_X_LIST
#undef X
};
然后,TYPE\u X\N
可以用来设置数组大小等。我想你的问题中有拼写错误<代码> UTI32 32 t < /COD> > >代码> Type XXT C或C++?挑一个。一个解决方案可能在另一个中不起作用。“但我还必须维护两段代码,而不是一段。”这是什么意思?@cdhowie c code。C++也可以用于自学,但在项目中可能不使用。<代码> y>类型x 我不认为你可以调用这样的东西。原因<代码>类型xxt 是一个结构,而不是数组是元素不共享同一类型……一点关系都没有?否则,这看起来像是使用多态性的典型情况。是的,它们是非常不同的类型。好吧,现在我觉得我的问题可能太奇怪了,也不典型:(