C/C++ 注意点补充

C/C++ 注意点补充

  • 地址与指针
  • 函数缺省
  • 结构体/指针内存分配与初始化

地址与指针

在这里插入图片描述

p的值是a的地址值,p的类型是int*p的值是十六进制表示的地址值
所以可以直接把地址值通过强制转换 转换为地址p 如上图!!!

int a=10;
int *p=&a;
printf("%#p\n",p);  // 0x61fe14
printf("%#x\n",p);  // 0x61fe14
printf("%#p\n",&a); // 0x61fe14

函数缺省

https://blog.csdn.net/xinger_28/article/details/83898804

// 是的,C语言中的函数不支持直接定义缺省参数。在你提供的代码中,函数DelayXms没有定义缺省参数。缺省参数只在一些高级编程语言中(如C++)中支持,用于为函数的参数提供默认值。在C语言中,如果你希望函数的某个参数具有默认值,你可以通过函数重载或者函数重写来实现类似的功能。但是需要注意的是,C语言并不直接支持缺省参数这个特性。

结构体/指针内存分配与初始化

typedef struct position_info_t {
  char timestr[64];    
  double longitude;    
  uint8_t isEash;      
  double latitude;     
  double height;       
  uint8_t hemisphere;  
  uint8_t satellites;  
} position_info_t, *pPosition_info_t;

typedef struct fan_info_t {
  uint16_t rate[4];  
} fan_info_t, *pFan_info_t;

typedef struct chassis_info_t {
  float temp;        
  uint8_t humidity; 
} chassis_info_t, *pChassis_info_t;

typedef struct interference_module_vol_info_t {
  uint16_t vol[5];  
} interference_module_vol_info_t, *pInterference_module_vol_info;

typedef struct interference_module_elem_info_t {
  uint16_t elem[5];  
} interference_module_elem_info_t, *pInterference_module_elem_info;

typedef struct compass_info_t {
  double DOA;   
  double decl;  
} compass_info_t, *pCompass_info;

typedef struct interference_module_ctrl_t {
  bool isOpen[5];
} interference_module_ctrl_t, *pInterference_module_ctrl_t;

typedef struct rFreq_switch_status_t {
  bool isOpen[7];
} rFreq_switch_status_t, *pRFreq_switch_status_t;

typedef struct Ptz_Horizontal_info_t {
  double HorizontalAngle;  
} Ptz_Horizontal_info_t, *pPtz_Horizontal_info_t;


typedef struct Ptz_vertical_info_t {
  double VerticalAngle;  
} Ptz_vertical_info_t, *pPtz_vertical_info_t;

typedef struct device_info{
    char szIp[50]; 
    int nPort;
    char szGateway[50];
    char szMask[50];
    char szMac[50];
    char szUid[50];
}device_info;

typedef struct  {
 uint16_t temp[4];
}fourTemp;

typedef struct GnssData
{
//    GNSSData() {}
    interference_module_vol_info_t *vol_info;   
    interference_module_elem_info_t *elem_info; 
    uint16_t *vol;                              
    chassis_info_t *chassis_info;               
    position_info_t *position_info;             
    interference_module_ctrl_t *module_ctrl;    
    rFreq_switch_status_t *rFreq_info;          
    fan_info_t  *fan_info;                      
    Ptz_Horizontal_info_t *ptzH_info;           
    Ptz_vertical_info_t *ptzV_info;             
    compass_info_t *compass_info;               
    fourTemp *fourTemp_info;                    
}GnssData, *pGnssData;


m_gnssData.vol_info=(interference_module_vol_info_t *)malloc(sizeof(interference_module_vol_info_t ));
m_gnssData.elem_info=(interference_module_elem_info_t*)malloc(sizeof(interference_module_elem_info_t));
m_gnssData.vol=(uint16_t*)malloc(sizeof(uint16_t));
m_gnssData.chassis_info=(chassis_info_t*)malloc(sizeof(chassis_info_t));
m_gnssData.position_info=(position_info_t*)malloc(sizeof(position_info_t));
m_gnssData.module_ctrl=(interference_module_ctrl_t*)malloc(sizeof(interference_module_ctrl_t));
m_gnssData.rFreq_info=(rFreq_switch_status_t*)malloc(sizeof(rFreq_switch_status_t));
m_gnssData.fan_info=(fan_info_t*)malloc(sizeof(fan_info_t));
m_gnssData.ptzH_info=(Ptz_Horizontal_info_t*)malloc(sizeof(Ptz_Horizontal_info_t));
m_gnssData.ptzV_info=(Ptz_vertical_info_t*)malloc(sizeof(Ptz_vertical_info_t));
m_gnssData.compass_info=(compass_info_t*)malloc(sizeof(compass_info_t));
m_gnssData.fourTemp_info=(fourTemp*)malloc(sizeof(fourTemp));

memset(m_gnssData.vol_info,0,sizeof(interference_module_vol_info_t));
memset(m_gnssData.elem_info,0,sizeof(interference_module_elem_info_t));
memset(m_gnssData.vol,0,sizeof(uint16_t));
memset(m_gnssData.chassis_info,0,sizeof(chassis_info_t));
memset(m_gnssData.position_info,0,sizeof(position_info_t));
memset(m_gnssData.module_ctrl,0,sizeof(interference_module_ctrl_t));
memset(m_gnssData.rFreq_info,0,sizeof(rFreq_switch_status_t));
memset(m_gnssData.fan_info,0,sizeof(fan_info_t));
memset(m_gnssData.ptzH_info,0,sizeof(Ptz_Horizontal_info_t));
memset(m_gnssData.ptzV_info,0,sizeof(Ptz_vertical_info_t));
memset(m_gnssData.compass_info,0,sizeof(compass_info_t));
memset(m_gnssData.fourTemp_info,0,sizeof(fourTemp));

你可能感兴趣的:(C/C++,c++,开发语言)