Estructura anidada con matriz de puntero de estructura

Estructura principal

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;
       unsigned u2DeviceType:2;
       unsigned u2RxOnWhenIdle:2;
       unsigned u2Relationship:3;
       unsigned u1Reserved1:1;
       unsigned u2PermitJoining:2;
       unsigned u6Reserved2:6;
    } ;
    uint8 au8Field[2];
 } uAncAttrs;
} ZPS_tsAplZdpDiscNtEntry;

i have defined ZPS_tsAplZdpMgmtLqiRsp *pointer;

this seems to be okay..


but how can i access those values inside the ZPS_tsAplZdpDiscNtEntry structure

preguntado el 02 de febrero de 12 a las 11:02

2 Respuestas

you get access to the array with: pointer->pNetworkTableList so from there you can access all elements of the structure..

e.g. access to the u64ExtPanId of the the element with index 0:

pointer->pNetworkTableList[0].u64ExtPanId = 1232;

Respondido 02 Feb 12, 15:02

if you have a stack dump, than you have not allocated memory for the structures, see Jurlies answer how to do so. - Diamante negro

You have the pointer, but you haven't the instance of the structure itself. Do the next:

ZPS_tsAplZdpMgmtLqiRsp *pointer = (ZPS_tsAplZdpMgmtLqiRsp *)malloc(sizeof(ZPS_tsAplZdpMgmtLqiRsp));

... and yes you should allocate the memory for pNetworkTableList as well:

pointer->pNetworkTableList = (ZPS_tsAplZdpDiscNtEntry *)malloc(sizeof(ZPS_tsAplZdpDiscNtEntry));

then you may


 pointer->pNetworkTableList->u8Status = 12; 

y así sucesivamente.

don't forget to do


in the end of work.

Respondido 02 Feb 12, 15:02

The return value of malloc shouldn't be cast and I would prefer calloc. - Diamante negro

I worked in pure C many years ago. I may forget some details :-). Is void* type implicitly casted to any other in C? - Jurlie

yes, many years ago that was necessary but nowadays it isn't. it's implictly casted to the correct type, which avoids code cluttering and wrong casts. See… - Diamante negro

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.