高手指教malloc用法,分配二维结构体指针。
相关代码如下:
struct s_Dip_Azimuth{
float fLine;
float fCmp;
float fTime;
float fDipX;
float fDipY;
float fAzimuth;
};
/*类型定义*/
typedef struct s_Dip_Azimuth sDA;
sDA **WhDataBuf1;
/*初始化*/
(49)*WhDataBuf1 = (sDA *)malloc(2300 * sizeof(sDA));
(50)WhDataBuf1 = (sDA *)malloc(1000 * sizeof(sDA));
程序编译时显示如下信息(VC++ 6.0环境下):
***.c(50) : warning C4047: '=' : 'struct s_Dip_Azimuth ** ' differs in levels of indirection from 'struct s_Dip_Azimuth *'
***.c(49) : warning C4700: local variable 'WhDataBuf1' used without having been initialized
请教高手,这个结构体指针应该如何初始化,并且最后如何释放内存(free函数)。
都不正确!
正确初始化方式是这样的:
WhDataBuf1 = (sDA **)malloc(2300 * sizeof(sDA));
for(i=0; i<2300; ++i) WhDataBuf1[i] = (sDA *)malloc(1000 * sizeof(sDA));