串口數(shù)據(jù)傳輸當(dāng)中的共用體和結(jié)構(gòu)體轉(zhuǎn)換
typedef union
{
float f;
unsigned char s[4];
}Union_test;
f的4個(gè)字節(jié)和s[4]的4個(gè)字節(jié)是共用一個(gè)區(qū)域,如果我們令f=231.5,然后通過VS的監(jiān)視窗查看s[4]的數(shù)值,下面是測(cè)試程序:
#include
//共用體
//float f;//4個(gè)字節(jié)
//char s[4];//4個(gè)字節(jié)
typedef union
{
float f;
unsigned char s[4];
}Union_test;
typedef struct st
{
float f1;
}Struct_test;
void main(void)
{
float a=231.5;
Union_test x;
Struct_test z;
x.f = a;
z = *(Struct_test *)(