大家好,欢迎来到IT知识分享网。
#include <string.h>
#include <stdio.h>
typedef unsigned char UINT8;
struct entity { char char0; char char1; char char2; char char3; unsigned short char4;};
union name{ char _name[6]; struct entity Fname;};
struct FLAG {
UINT8 bit0 : 1;
UINT8 bit1 : 1;
UINT8 bit2 : 1;
UINT8 bit3 : 1;
UINT8 bit4 : 1;
UINT8 bit5 : 1;
UINT8 bit6 : 1;
UINT8 bit7 : 1;
};
// 联合体里面套用结构体
union FLAGS { UINT8 byte; struct FLAG bit;};
union name myname;
volatile union FLAGS net_flag;
#define DEV_DIS net_flag.bit.bit0
#define ON 1
void main() {
char *test = "abcdA";
char _test[] = "abcdA";
char __test[6] = "abcdA";
// 分别输出串长度、指针地址size、[]声明字符串会自动加\0结束符,即长度自动加1
printf("test strlen :%d sizeof(test) :%d sizeof(_test) :%d\n",strlen(test),sizeof(test), sizeof(_test));
// [6]声明字符串strlen取决于长度,sizeof取决于[]分配的大小,如使用memset的时候注意两个函数的区别
printf("__test strlen :%d sizeof(test) :%d\n", strlen(__test), sizeof(__test));
strcpy_s(myname._name,sizeof(test), test);
printf("sizeof char, unsigned short, int :%d, %d, %d\n", sizeof(char),sizeof(unsigned short),sizeof(int));
printf("myname size :%d\n",sizeof(myname));
printf("myname._name size :%d\n", sizeof(myname._name));
printf("myname.Fname size :%d\n", sizeof(myname.Fname));
printf("myname.Fname include :%c %c %c %c %c\n", myname.Fname.char0, myname.Fname.char1, myname.Fname.char2, myname.Fname.char3, myname.Fname.char4);
/* 联合体的特性 */
union FLAGS flags;
flags.byte = 8;
printf(" flags 's size :%d value :%d \n",sizeof(flags.bit),flags.bit.bit2);
flags.bit.bit0 = 0;
flags.bit.bit1 = 0;
flags.bit.bit2 = 1;
flags.bit.bit3 = 1;
printf(" flags 's size :%d value :%d", sizeof(flags.bit), flags.byte);
}
免责声明:本站所有文章内容,图片,视频等均是来源于用户投稿和互联网及文摘转载整编而成,不代表本站观点,不承担相关法律责任。其著作权各归其原作者或其出版社所有。如发现本站有涉嫌抄袭侵权/违法违规的内容,侵犯到您的权益,请在线联系站长,一经查实,本站将立刻删除。 本文来自网络,若有侵权,请联系删除,如若转载,请注明出处:https://yundeesoft.com/15197.html