是否可以定义不同元素类型的关联数组-如下所示?
typedef enum {M1, M2} mod_t;
typedef struct {...} m1_reg_t;
typedef struct {...} m2_reg_t;
array[mod_t] = {
M1: { string name;
m1_reg_t cfg
}
M2: { string name;
m2_reg_t cfg;
int m2_indx;
}
}
否,所有数组必须具有相同的元素类型。
如果您真的想要只包含两个具有不同类型的元素的数组,请使用其他结构而不是数组。
typedef struct {
string name;
m1_reg_t cfg;
} M1_t;
typedef struct {
string name;
m2_reg_t cfg;
int m2_indx;
} M2_t;
struct { M1_t M1;
M2_t M2;
} opcodes;
但是,如果要查找包含许多元素的数组,其中每个元素可能拥有不同的操作码,请考虑一个带有标记的并集的动态数组。
typedef union tagged { struct { string name; m1_reg_t cfg; } M1; struct { string name; m2_reg_t cfg; int m2_indx; } M2; } opcode_t; opcode_t opcodes[];
访问带标签的联合需要模式匹配语句。请参阅1800-2017 LRM中的12.6节。