C 带结构指针数组的嵌套结构
主体结构C 带结构指针数组的嵌套结构,c,pointers,structure,zigbee,C,Pointers,Structure,Zigbee,主体结构 typedef struct { uint8 u8Status; uint8 u8NeighborTableEntries; uint8 u8StartIndex; uint8 u8NeighborTableListCount; /* Rest of the message is variable length */ ZPS_tsAplZdpDiscNtEntry* pNetworkTableList;
typedef struct {
uint8 u8Status;
uint8 u8NeighborTableEntries;
uint8 u8StartIndex;
uint8 u8NeighborTableListCount;
/* Rest of the message is variable length */
ZPS_tsAplZdpDiscNtEntry* pNetworkTableList;
//pNetworkTableList is a pointer to
//the first
//entry in the list of reported
//Neighbour table entries
} ZPS_tsAplZdpMgmtLqiRsp;
typedef struct
{
uint64 u64ExtPanId;
uint64 u64ExtendedAddress;
uint16 u16NwkAddr;
uint8 u8LinkQuality;
uint8 u8Depth;
union
{
struct
{
unsigned u2DeviceType:2;
unsigned u2RxOnWhenIdle:2;
unsigned u2Relationship:3;
unsigned u1Reserved1:1;
unsigned u2PermitJoining:2;
unsigned u6Reserved2:6;
} ;
uint8 au8Field[2];
} uAncAttrs;
} ZPS_tsAplZdpDiscNtEntry;
我已经定义了ZPS_tsAplZdpMgmtLqiRsp*指针
这似乎没问题
pointer->u8Status
pointer->u8NeighborTableEntries
pointer->u8StartIndex
pointer->u8NeighborTableListCount
但是我如何访问ZPS_tsaplzdpdiscontentry结构中的那些值您可以通过:
指针->pNetworkTableList
typedef struct {
uint8 u8Status;
uint8 u8NeighborTableEntries;
uint8 u8StartIndex;
uint8 u8NeighborTableListCount;
/* Rest of the message is variable length */
ZPS_tsAplZdpDiscNtEntry* pNetworkTableList;
//pNetworkTableList is a pointer to
//the first
//entry in the list of reported
//Neighbour table entries
} ZPS_tsAplZdpMgmtLqiRsp;
typedef struct
{
uint64 u64ExtPanId;
uint64 u64ExtendedAddress;
uint16 u16NwkAddr;
uint8 u8LinkQuality;
uint8 u8Depth;
union
{
struct
{
unsigned u2DeviceType:2;
unsigned u2RxOnWhenIdle:2;
unsigned u2Relationship:3;
unsigned u1Reserved1:1;
unsigned u2PermitJoining:2;
unsigned u6Reserved2:6;
} ;
uint8 au8Field[2];
} uAncAttrs;
} ZPS_tsAplZdpDiscNtEntry;
因此,您可以从那里访问结构的所有元素
e、 g.访问索引为0的元素的u64ExtPanId:
pointer->pNetworkTableList[0].u64ExtPanId = 1232;
您有指针,但没有结构本身的实例。 做下一步:
ZPS_tsAplZdpMgmtLqiRsp *pointer = (ZPS_tsAplZdpMgmtLqiRsp *)malloc(sizeof(ZPS_tsAplZdpMgmtLqiRsp));
。。。是的,您也应该为pNetworkTableList分配内存:
pointer->pNetworkTableList = (ZPS_tsAplZdpDiscNtEntry *)malloc(sizeof(ZPS_tsAplZdpDiscNtEntry));
那么你可以
做
等等
别忘了做点什么
free(pointer->pNetworkTableList);
free(pointer);
在工作结束时。如果您有堆栈转储,而没有为结构分配内存,请参阅陪审员回答如何执行。malloc的返回值不应强制转换,我更喜欢calloc。我多年前在纯C中工作。我可能会忘记一些细节:-)。void*类型在C中是否隐式转换为其他类型?是的,许多年前这是必要的,但现在不是。它隐式地转换为正确的类型,从而避免了代码混乱和错误的转换。看见