c中同一结构的多个名称
是否可以在C中为同一结构创建多个名称? 其思想是,具有相同布局但名称不同的通信协议中的数据结构可以共享相同的定义 例如:c中同一结构的多个名称,c,inheritance,struct,typedef,C,Inheritance,Struct,Typedef,是否可以在C中为同一结构创建多个名称? 其思想是,具有相同布局但名称不同的通信协议中的数据结构可以共享相同的定义 例如: struct flags_type1 { uint8_t flag1; uint8_t flag2; }; struct flags_type2 { uint8_t flag1; uint8_t flag2; }; /* The flag layout for type 2 is identic
struct flags_type1 {
uint8_t flag1;
uint8_t flag2;
};
struct flags_type2 {
uint8_t flag1;
uint8_t flag2;
};
/* The flag layout for type 2 is identical to type 1, so somehow
* just reuse the struct for type 1, but with a new name.
* (For readability in the implementation. */
/* *** HERE: Insert solution code here. Something with typedef? *** */
struct flags_type3 {
uint8_t flag1;
uint8_t flag2;
uint8_t flag3;
};
struct msg_object {
uint8_t type_id_code;
union {
struct flags_type1 type1;
struct flags_type2 type2;
struct flags_type3 type3;
} flags;
uint8_t payload[7];
};
/* Utilization: */
struct msg_object *msg;
switch (msg->type_id_code) {
case TYPE1:
do_things_type1(msg->flags.type1);
break;
case TYPE2:
do_things_type2(msg->flags.type2);
break;
case TYPE3:
do_things_type3(msg->flags.type3);
break;
}
动机:
我正在实现一个通信协议,它可以传输不同类型的消息对象(特别是CANopen中的SDO)。每个对象都有一个5位的状态标志字段,其中一些对象的此字段布局相同。还有一个3位字段,用于标识对象类型。这两个字段被装入一个字节
因此,其思想是基于类型标识符利用正确的标志布局。为了使标记布局的选择更直观,在不定义同一布局两次的情况下显示所有类型名称似乎是明智的
从技术上讲,我认为这是一个继承问题
//奥登我不确定你到底想达到什么目的。如果我没有误解这一点,我想这可以通过
typedef
实现。比如:
struct flags_type {
uint8_t flag1;
uint8_t flag2;
};
typedef struct flags_type type1;
typedef struct flags_type type2;
我不确定你到底想达到什么目的。如果我没有误解这一点,我想这可以通过
typedef
实现。比如:
struct flags_type {
uint8_t flag1;
uint8_t flag2;
};
typedef struct flags_type type1;
typedef struct flags_type type2;
不能有两个带有不同标记(struct/union/enum关键字后面的内容)的标记类型(标记类型是结构、联合或枚举) 指向同一类型(您可以将
struct x
看作是指向类型定义的编译时指针)。换句话说,struct x
永远不能别名struct y
。但是您可以让不同的typedefs
指向同一类型
typedef struct flags_type1 {
uint8_t flag1;
uint8_t flag2;
} flags_type1; //flags_type1 is now both a tag and a global typename
typedef flags_type1 flags_type2; //flags_type2 == flags_type1
您可能希望flags\u type1
和flags\u type2
具有不同的类型,但是(为了函数),在纯C中,您可以执行以下操作:
struct flags2 { struct flags1 embedded; }
struct flags2 { struct flags1; }
//w/ -fms-extensions, reuseses the body and
//makes struct flags2 implicitly convertible to struct flags1
这样,您就必须提到成员名称(嵌入)才能访问成员。这在straight C中是不可避免的(如果您不想对成员集使用宏),但在带有-fms扩展名的gcc/clang上,您可以执行以下操作:
struct flags2 { struct flags1 embedded; }
struct flags2 { struct flags1; }
//w/ -fms-extensions, reuseses the body and
//makes struct flags2 implicitly convertible to struct flags1
然后直接访问成员
除此之外,还有宏。不能有两个标记类型(标记类型是结构、联合或枚举)具有不同的标记(在struct/union/enum关键字之后是什么)
指向同一类型(您可以将struct x
看作是指向类型定义的编译时指针)。换句话说,struct x
永远不能别名struct y
。但是您可以让不同的typedefs
指向同一类型
typedef struct flags_type1 {
uint8_t flag1;
uint8_t flag2;
} flags_type1; //flags_type1 is now both a tag and a global typename
typedef flags_type1 flags_type2; //flags_type2 == flags_type1
您可能希望flags\u type1
和flags\u type2
具有不同的类型,但是(为了函数),在纯C中,您可以执行以下操作:
struct flags2 { struct flags1 embedded; }
struct flags2 { struct flags1; }
//w/ -fms-extensions, reuseses the body and
//makes struct flags2 implicitly convertible to struct flags1
这样,您就必须提到成员名称(嵌入)才能访问成员。这在straight C中是不可避免的(如果您不想对成员集使用宏),但在带有-fms扩展名的gcc/clang上,您可以执行以下操作:
struct flags2 { struct flags1 embedded; }
struct flags2 { struct flags1; }
//w/ -fms-extensions, reuseses the body and
//makes struct flags2 implicitly convertible to struct flags1
然后直接访问成员
除此之外,还有宏。您不需要typedef,您可以嵌套结构定义:
处理程序的代码:
您不需要typedef,并且可以嵌套结构定义:
处理程序的代码:
问题是什么?顺便说一句:switch(msg->type_id_code&7){
或根据位字段的顺序:switch(msg->type_id_code>>5){
并注意您的uint8_t有效载荷[7]
将始终假定处于相同的偏移量,与msgtype无关。因此,结构标志type1和结构标志type2是相同的,因此我希望能够执行类似于typedef结构标志type1结构标志type2的操作;而不是定义新的结构标志type2
您可以为标志type1
创建别名edflags_type2
:typedef结构标志_type1 flags_type2;
@auduknudsroed[IMHO]在真正需要之前尽量避免使用typedef。在大多数情况下,您只需要结构定义。同一事物的名称太多只会让人困惑。typedef解决方案的缺点是它混淆了struct关键字,因此我得到了union{struct flags_type1;flags_type2;struct flags_type3;}问题是什么?顺便说一句:switch(msg->type_id_code&7){
或根据位字段的顺序:switch(msg->type_id_code>>5){
,请注意您的uint8\t有效载荷[7]
将始终假定处于相同的偏移量,与msgtype无关。因此,结构标志type1和结构标志type2是相同的,因此我希望能够执行类似于typedef结构标志type1结构标志type2的操作;而不是定义新的结构标志type2
您可以为标志type1
创建别名edflags_type2
:typedef结构标志_type1 flags_type2;
@auduknudsroed[IMHO]在真正需要之前尽量避免使用typedef。在大多数情况下,您只需要结构定义。同一事物的名称太多只会让人困惑。typedef解决方案的缺点是它混淆了struct关键字,因此我得到了union{struct flags_type1;flags_type2;struct flags_type3;}flags;您还可以编写一个用法示例,如type1f1;f1.flag1=…
您还可以编写一个用法示例,如type1f1;f1.flag1=…
“low,struct x不能别名struct y”谢谢,将struct x别名为struct y正是我想要做的,因此这回答了问题。“噢,struct x永远不能将struct y别名”谢谢,将struct x别名为struct y正是我想要做的,所以这回答了问题。