[英]Nested structure with array of structure pointer
主要結構
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;
這似乎還可以。
pointer->u8Status
pointer->u8NeighborTableEntries
pointer->u8StartIndex
pointer->u8NeighborTableListCount
但是我如何訪問ZPS_tsAplZdpDiscNtEntry結構內的那些值
您可以使用以下pointer->pNetworkTableList
訪問數組: pointer->pNetworkTableList
以便從那里可以訪問結構的所有元素。
例如,訪問索引為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));
那你可能
做
pointer->pNetworkTableList->u8Status = 12;
等等。
別忘了做
free(pointer->pNetworkTableList);
free(pointer);
在工作的最后。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.