char *str;似乎把这个写进结构体然后调用malloc()就报错了,各位有遇到过吗?是不是这样啊?
结构体里包含指针类型时,动态申请结构体内存并不会报错,但是在使用之前要对这个指针类型申请指向的内存。
我试了一下没报错啊!你是不是其他地方写的有问题?
#include "stdafx.h"
#include
#include
typedef unsigned int UINT32;
typedef unsigned short USHORT;
typedef unsigned char UCHAR;
typedef struct tagTest
{
USHORT usA;
USHORT usB;
UINT32 uiC;
UCHAR *pucBuff;
}Test_S;
int _tmain(int argc, _TCHAR* argv[])
{
Test_S *pstTest = NULL;
pstTest = (Test_S*)malloc(sizeof(Test_S));
if (NULL == pstTest)
{
return 1;
}
pstTest->pucBuff = (UCHAR*)malloc(sizeof(UINT32));
if (NULL == pstTest->pucBuff)
{
return 1;
}
*((UINT32*)(pstTest->pucBuff)) = 0x12345678;
printf("0x%x", *((UINT32*)(pstTest->pucBuff)));
free(pstTest->pucBuff);
free(pstTest);
getchar();
return 0;
}