1 #include <stdio.h> 2 3 typedef struct MenuItem{//28字节 4 5 char cursorPosition; //1 byte 6 unsigned char menuItemTotal; //1 byte 7 bool isSelect; //1 byte 8 //此处自动补充1个字节,字节对齐 9 const unsigned char *icoSelected; //4 byte 10 const unsigned char *icoUnselected; //4 byte 11 const char *title; //4 byte 12 void (*Function)(void); //4 byte 13 struct MenuItem* parentMenu; //4 byte 14 struct MenuItem* childrenMenu; //4 byte 15 }MenuItem_typedef; 16 17 18 const char* mainMenuLanguage[3][10] = { 19 {"1.参数设置","2.密码修改","3.退出","4.退出","5.退出","6.退出","7.退出","8.退出","9.退出","10.退出",}, 20 {"1.param set","2.password modify","3.exit","4.exit","5.exit","6.exit","7.exit","8.exit","9.exit","10.exit",}, 21 {"1.exit","2.exit","3.exit","4.exit","5.exit","6.exit","7.exit","8.exit","9.exit","10.exit",}, 22 }; 23 24 25 //变量声明 26 MenuItem_typedef menuL1_dev[10]; 27 MenuItem_typedef* pCurItem; 28 const char * pCurTitle; 29 30 int main(void) 31 { 32 for(int i=0; i<10; i++) 33 { 34 menuL1_dev[i].cursorPosition = NULL; 35 menuL1_dev[i].menuItemTotal = 10; 36 menuL1_dev[i].isSelect = false; 37 menuL1_dev[i].icoSelected = NULL;//ico 38 menuL1_dev[i].icoUnselected = NULL; 39 menuL1_dev[i].title = mainMenuLanguage[0][i]; 40 menuL1_dev[i].Function = NULL; 41 menuL1_dev[i].parentMenu = NULL; 42 menuL1_dev[i].childrenMenu = NULL; 43 } 44 45 printf("%d \n", sizeof(const unsigned char *));//4字节 46 printf("%d \n", sizeof(void (*)(void)));//函数指针4字节 47 printf("%d \n", sizeof(bool));//1字节 48 printf("%d \n", sizeof(struct MenuItem));//28字节 49 printf("%d \n", sizeof(struct MenuItem *));//4字节 50 printf("%d \n", sizeof(menuL1_dev));//280字节 51 52 printf("menuL1_dev addr=%d \n", menuL1_dev);//addr1 = 4339288 53 pCurItem = menuL1_dev + 1; 54 printf("pCurItem addr=%d \n", pCurItem);//addr2= addr1+28 = 4339316 55 56 pCurTitle = menuL1_dev->title + 2; 57 printf("pCurTitle=%s \n", pCurTitle);//参数设置 58 59 pCurTitle = (menuL1_dev+1)->title + 4; 60 printf("pCurTitle=%s \n", pCurTitle);//码设置 61 62 return 0; 63 }
结构体应用及其字节对齐问题
猜你喜欢
转载自www.cnblogs.com/eruca520/p/9176336.html
今日推荐
周排行