干貨分享 | C語言的聯(lián)合體

原創(chuàng) 2021-03-05 09:04:00 C語言聯(lián)合體

1、聯(lián)合體介紹

我們知道結(jié)構(gòu)體(Struct)是一種構(gòu)造類型或復雜類型,它可以包含多個類型不同的成員。在C語言 中,還有另外一種和結(jié)構(gòu)體非常類似的語法,叫做共用體(Union),它的定義格式為:

union 共用體名{ 
成員列表
 };

共用體有時也被稱為聯(lián)合或者聯(lián)合體,這也是 Union 這個單詞的本意。

結(jié)構(gòu)體和共用體的區(qū)別在于:結(jié)構(gòu)體的各個成員會占用不同的內(nèi)存,互相之間沒有影響;而共用體的所

有成員占用同一段內(nèi)存,修改一個成員會影響其余所有成員。

結(jié)構(gòu)體占用的內(nèi)存大于等于所有成員占用的內(nèi)存的總和(成員之間可能會存在縫隙),共用體占用的內(nèi)

存等于最長的成員占用的內(nèi)存。共用體使用了內(nèi)存覆蓋技術,同一時刻只能保存一個成員的值,如果對

新的成員賦值,就會把原來成員的值覆蓋掉。


#include "stdafx.h"
#include <stdlib.h>
#include <stdio.h> 
union data{ 
   int n; 
   char ch; 
   short m; 
};
int main(){ 
union data a; 
printf("sizeof(a) = %d\n", sizeof(a));
a.n = 0x11;
printf("n=%X, ch=%X, m=%X\n", a.n, a.ch, a.m);
a.ch = 0x66;
printf("n=%X, ch=%X, m=%X\n", a.n, a.ch, a.m); 
a.m = 0x5577; 
printf("n=%X, ch=%X, m=%X\n", a.n, a.ch, a.m); 
a.n = 0x11226677; 
printf("n=%X, ch=%X, m=%X\n", a.n, a.ch, a.m); 
system("pause"); 
return 0; }

運行結(jié)果:

運行結(jié)果


2 、結(jié)構(gòu)體冒號的用法

結(jié)構(gòu)體中的冒號表示位域,位域出現(xiàn)的原因是由于某些信息的存儲表示只需要幾個bit位就可以表示而不 需要一個完整的字節(jié),同時也是為了節(jié)省存儲空間和方便處理。

其表示形式為:



struct 位域結(jié)構(gòu)名
{
類型說明符	位域名:位域長度
}

其表示形式為:

struct  bit_struct
{
int  bit1:3;
int  bit2:5;
int  bit3:7;
}data;

其中bit_struct表示位域結(jié)構(gòu)體,bit1、bit2、bit3表示對應的位域,data表示位域結(jié)構(gòu)體定義的變量。整個位域結(jié)構(gòu)體占用2個字節(jié),bit1占3位,bit2占5位,bit1和bit2共用一個字節(jié),bit3占7位,獨占一個字節(jié)


#include "stdafx.h" 
#include <stdlib.h> 
#include <stdio.h>
struct  bit_struct 
{
  char  bit1:3;
  char  bit2:5;
  char  bit3:7;
}data;
int main()
{
   data.bit1 = 0xAA;
  data.bit2 = 0xAA;
  data.bit3 = 0xAA;
 printf("sizeof(data) = %x \n data.bit1 = %x \n data.bit2 = %x \n data.bit3 = %x \n",sizeof(bit_struct),data.bit1,data.bit2,data.bit3);
  system("pause");
  return 0;
}


運行結(jié)果:

查看運行結(jié)果

3、聯(lián)合使用

舉一個MCP2518FD芯片的例子:先看一下CAN幀格式:

CAN幀格式


//占用4個字節(jié)
typedef struct _CAN_MSGOBJ_ID {
uint32_t SID : 11;
uint32_t EID : 18;uint32_t SID11 : 1;
uint32_t unimplemented1 : 2;
} CAN_MSGOBJ_ID;
//占用4個字節(jié)
typedef struct _CAN_TX_MSGOBJ_CTRL {
uint32_t DLC : 4;
uint32_t IDE : 1;
uint32_t RTR : 1;
uint32_t BRS : 1;
uint32_t FDF : 1;
uint32_t ESI : 1;
#ifdef MCP2517FD
uint32_t SEQ : 7;
uint32_t unimplemented1 : 16;
#else
uint32_t SEQ : 23;
#endif
} CAN_TX_MSGOBJ_CTRL;
//占用4個字節(jié)
typedef uint32_t CAN_MSG_TIMESTAMP;//沒有用到
typedef union _CAN_TX_MSGOBJ {
struct {
CAN_MSGOBJ_ID id; //占4個字節(jié)
CAN_TX_MSGOBJ_CTRL ctrl; //占4個字節(jié)
CAN_MSG_TIMESTAMP timeStamp;//占4個字節(jié)
} bF; //共享12個字節(jié)
uint32_t word[3];//共享12個字節(jié)
uint8_t byte[12];//共享12個字節(jié)
} CAN_TX_MSGOBJ;
txObj.bF.id.SID = CAN_TX_ID;
txObj.bF.ctrl.DLC = CAN_DLC_4;//發(fā)送的數(shù)據(jù)長度
txObj.bF.ctrl.IDE = 0;//標識符擴展位,在擴展幀中恒為隱性1,在標準幀中,IDE位于控制段,且
恒為顯性0
txObj.bF.ctrl.BRS = 0;//BRS(Bit Rate Switch)位速率轉(zhuǎn)換開關,當BRS為顯性位時數(shù)據(jù)段的位
速率與仲裁段的位速率一致,當BRS為隱性位時數(shù)據(jù)段的位速率高于仲裁段的位速率
txObj.bF.ctrl.FDF = 0;//擴展數(shù)據(jù)長度,在標準的CAN幀中,控制場包含的保留位被指定為顯性位發(fā)
送,但是在CAN-FD幀中以隱性位發(fā)送,主要用于區(qū)分標準CAN幀格式和CAN-FD的幀格式
n = DRV_CANFDSPI_DlcToDataBytes(CAN_DLC_4);
for (i = 0; i < n; i++)
{
txd[i] = Count;
Count++;
}uint8_t txBuffer[MAX_MSG_SIZE];
txBuffer[0] = txObj->byte[0]; //not using 'for' to reduce no of instructions
txBuffer[1] = txObj->byte[1];
txBuffer[2] = txObj->byte[2];
txBuffer[3] = txObj->byte[3];
txBuffer[4] = txObj->byte[4];
txBuffer[5] = txObj->byte[5];
txBuffer[6] = txObj->byte[6];
txBuffer[7] = txObj->byte[7];
uint8_t i;
for (i = 0; i < txdNumBytes; i++)
{
txBuffer[i + 8] = txd[i];
}
// Make sure we write a multiple of 4 bytes to RAM
uint16_t n = 0;
uint8_t j = 0;
if (txdNumBytes % 4)
{
// Need to add bytes
n = 4 - (txdNumBytes % 4);
i = txdNumBytes + 8;
for (j = 0; j < n; j++)
{
txBuffer[i + 8 + j] = 0;
}
}
spiTransferError = DRV_CANFDSPI_WriteByteArray(index, a, txBuffer,
txdNumBytes + 8 + n);
if (spiTransferError)
{
return -4;
}
// Set UINC and TXREQ
spiTransferError = DRV_CANFDSPI_TransmitChannelUpdate(index, channel,
flush);
if (spiTransferError)
{
return -5;
}
return spiTransferError;

相關產(chǎn)品 >

  • OKT507-C 開發(fā)板

    CPU: T507 工業(yè)級處理器,超低功耗,10年+生命周期|推薦全志工業(yè)級T507開發(fā)板系列,飛凌OKT507-C開發(fā)板采用全志T507 四核工業(yè)級處理器 T507設計開發(fā),Cortex-A53架構(gòu),工業(yè)級寬溫,性能強,低功耗,是一款高性價比的工業(yè)級產(chǎn)品,提供豐富的開發(fā)設計資料,提供產(chǎn)品規(guī)格書,軟硬件手冊等,全志的T507適用于車載電子、電力、醫(yī)療、工業(yè)控制、物聯(lián)網(wǎng)、智能終端等領域。

    了解詳情
    OKT507-C 開發(fā)板
  • OK3568-C開發(fā)板

    強而穩(wěn),國產(chǎn)芯,1Tops算力,多路高速接口|飛凌RK3568系列RK3568開發(fā)板基于國產(chǎn)工業(yè)級AI處理器RK3568四核64位Cortex-A55 處理器設計。RK3568作為國產(chǎn)化高性能處理器,瑞芯微RK3568芯片是一款定位中高端的通用型SoC,瑞芯微RK3568芯片是一款定位中高端的通用型SoC,NPU達到1Tops,飛凌RK3568系列核心板提供瑞芯微RK3568規(guī)格書_datasheet_數(shù)據(jù)手冊_原理圖等,


    了解詳情
    OK3568-C開發(fā)板

推薦閱讀 換一批 換一批