zoukankan      html  css  js  c++  java
  • 结构体应用及其字节对齐问题

    在单片机中,结构体操作及其字节对齐问题(以LCD菜单为例)

     #include <stdio.h>
     
     typedef struct MenuItem {//28字节
     
       char cursorPosition;                //1 byte
       unsigned char menuItemTotal;        //1 byte
       bool isSelect;                      //1 byte
       //此处自动补充1个字节,字节对齐
       const unsigned char *icoSelected;   //4 byte
       const unsigned char *icoUnselected; //4 byte
       const char *title;                  //4 byte
       void (*Function)(void);             //4 byte
       struct MenuItem* parentMenu;        //4 byte
       struct MenuItem* childrenMenu;      //4 byte
     }MenuItem_typedef;
     
     
     const char* mainMenuLanguage[3][10] = {
       {"1.参数设置","2.密码修改","3.退出","4.退出","5.退出","6.退出","7.退出","8.退出","9.退出","10.退出",},
       {"1.param set","2.password modify","3.exit","4.exit","5.exit","6.exit","7.exit","8.exit","9.exit","10.exit",},
       {"1.參數設置","2.密碼修改","3.退出","4.exit","5.exit","6.exit","7.exit","8.exit","9.exit","10.exit",},
     };
     
     
     //变量声明
     MenuItem_typedef  menuL1_dev[10];
     MenuItem_typedef* pCurItem;
     const char * pCurTitle;
     
     int main(void)
     {
       for(int i=0; i<10; i++)
       {
         menuL1_dev[i].cursorPosition = NULL;
         menuL1_dev[i].menuItemTotal = 10;
         menuL1_dev[i].isSelect = false;
         menuL1_dev[i].icoSelected = NULL;//ico
         menuL1_dev[i].icoUnselected = NULL;
         menuL1_dev[i].title = mainMenuLanguage[0][i];
         menuL1_dev[i].Function = NULL;
         menuL1_dev[i].parentMenu = NULL;
         menuL1_dev[i].childrenMenu = NULL;
       }
     
       printf("%d 
    ", sizeof(const unsigned char *));//4字节
       printf("%d 
    ", sizeof(void (*)(void)));//函数指针4字节
       printf("%d 
    ", sizeof(bool));//1字节
       printf("%d 
    ", sizeof(struct MenuItem));//28字节
       printf("%d 
    ", sizeof(struct MenuItem *));//4字节
       printf("%d 
    ", sizeof(menuL1_dev));//280字节
     
       printf("menuL1_dev addr=%d 
    ", menuL1_dev);//addr1 = 4339288
       pCurItem = menuL1_dev + 1;
       printf("pCurItem addr=%d 
    ", pCurItem);//addr2= addr1+28 = 4339316
     
       pCurTitle = menuL1_dev->title + 2;
       printf("pCurTitle=%s 
    ", pCurTitle);//参数设置
     
       pCurTitle = (menuL1_dev+1)->title + 4;
       printf("pCurTitle=%s 
    ", pCurTitle);//码设置
     
       return 0;
     }
     
  • 相关阅读:
    基于selenium的web自动化框架
    spring boot整合Mybatis
    Spring boot进阶-配置Controller、interceptor...
    spring boot 第一个Demo
    Spring事务管理
    mybatis、Spring整合(eclipse)以及事务管理
    highcharts 根据表格转化为不同的图表
    highChartTable 切换
    highcharts 切换
    九度 1533 最长上升子序列
  • 原文地址:https://www.cnblogs.com/eruca520/p/9176336.html
Copyright © 2011-2022 走看看