1、请看下面的一段代码
typedef struct PVS_SET_TIME_USER_TIME_GROUP{ /*时段组下发*/
INT timePrdNum;
PVS_TIMEPRD timeprd[];
}PVS_SET_TIME_USER_TIME_GROUP;
这里的结构体内部用[]定义了一个指针,但是使用下面的代码会出现提示不兼容:
m_timegroup->timeprd = (PVS_TIMEPRD *)malloc(sizeof(PVS_TIMEPRD)*Num);
结果如下:
g++ Pvs_ProtocolAnalysis.cpp test_proto.cpp -g -o test_protocol
Pvs_ProtocolAnalysis.cpp: In member function ‘INT Pvs_ProtocolAnalysis::Pvs_inforDownloadIdlistChck(CHAR*, PVS_INFOR_DOWNLOAD_IDLIST_CHCK*)’:
Pvs_ProtocolAnalysis.cpp:835: error: incompatible types in assignment of ‘PVS_DOWNLOAD_MESSAGE*’ to ‘PVS_DOWNLOAD_MESSAGE [0]’
Pvs_ProtocolAnalysis.cpp: In member function ‘INT Pvs_ProtocolAnalysis::Pvs_SetTimeUserTimeGroup(CHAR*, PVS_SET_TIME_USER_TIME_GROUP*)’:
Pvs_ProtocolAnalysis.cpp:1141: error: incompatible types in assignment of ‘PVS_TIMEPRD*’ to ‘PVS_TIMEPRD [0]’
修改方法是:将结构体内的 PVS_TIMEPRD timeprd[] 换成 PVS_TIMEPRD *timeprd;
个人的意见是:因为timeprd[]本身毕竟还是个数组,数组不能够被重新分配,数组的地址也是不能够变化的,但是指针就不同了,指针是可以指向不同的地址的。