简体   繁体   English

将结构/工会成员链接到另一个结构/工会成员

[英]Linking struct/union member to another struct/union member

I am somewhat new to C in regards to structures, unions and bitfields and need help. 在结构,联合和位域方面,我对C有点陌生,需要帮助。 Below is the SLPH_CONTROL which will "control" the bits in the destination of DRVCONF0 and DRVCONF1. 下面是SLPH_CONTROL,它将“控制” DRVCONF0和DRVCONF1的目标位。 After the code snippet I will explain what I need. 在代码片段之后,我将解释我需要什么。

/* SLPH_CONTROL **********************************************/

typedef union
{
    struct
    {
        unsigned int SH0_BIT0 :1;
        unsigned int SH0_BIT1 :1;
    };
    uint8 SLPH_CONTROL_byte;
} SLPH_CONTROL;

SLPH_CONTROL my_SLPH_CONTROL;

/* DRVCONF0 **********************************************/

typedef union
{
    struct
    {
        unsigned int DF0_BIT0 :1;
        unsigned int DF0_BIT1 :1;
        unsigned int DF0_BIT2 :1;
        unsigned int DF0_BIT3 :1;
        unsigned int DF0_BIT4 :1;
    };
    uint8 DRVCONF0_byte;
} DRVCONF0;

DRVCONF0 my_DRVCONF0;

/* DRVCONF0 **********************************************/

typedef union
{
    struct
    {
        unsigned int DF1_BIT0 :1;
        unsigned int DF1_BIT1 :1;
        unsigned int DF1_BIT2 :1;
        unsigned int DF1_BIT3 :1;
        unsigned int DF1_BIT4 :1;
    };
    uint8 DRVCONF1_byte;
} DRVCONF1;

DRVCONF1 my_DRVCONF1;

Now with the code above I want to link or copy data from and to like so: 现在,使用上面的代码,我要像这样链接或复制数据:

SH0_BIT0:1 to DF0_BIT4:1;
SH1_BIT1:1 to DF1_BIT0:1;

Is there a way to do this? 有没有办法做到这一点? I am really lost and looked everywhere online for a solution to this. 我真的迷失了方向,在网上到处寻找解决方案。 Any suggestions are welcome!!! 任何建议都欢迎!!!

Thanks, 谢谢,

Eric 埃里克

Maybe this is possible on special embedded H/W (I don't know) but not on a regular PC with standard C. 也许这可以在特殊的嵌入式硬件(我不知道)上实现,但是在带有标准C的常规PC上则不可能。

Bit fields describe individual bits in one or more consecutive bytes. 位字段描述一个或多个连续字节中的各个位。 Actually, bytes are addressable but bits are not. 实际上,字节是可寻址的,而位是不可寻址的。 The read/write access to bits is internally solved by bit-wise operations (like in C expressed with & , | , ^ , ~ ). 对位的读/写访问在内部通过按位操作来解决(例如在用&|^~表示的C语言中)。

my_DRVCONF0 and my_DRVCONF1 are individual variables in your sample. my_DRVCONF0my_DRVCONF1是示例中的各个变量。 Thus, it's even not granted that they will be allocated/stored in consecutive memory. 因此,甚至不准将它们分配/存储在连续的内存中。 (They definitely will not if some kind of word-alignment is enabled.) (如果启用某种字对齐功能,他们肯定不会。)

You probably have to make the control more sophisticated (eg providing pairs of address and bit index) to do what's intended. 您可能必须使控件更加复杂(例如,提供地址和位索引对),才能完成预期的工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM