Search code examples
carrayspointersstructure

array to structure casting


I have these three structures,

typedef struct serial_header {
    int zigbeeMsgType;
    int seqNumber;
    int commandIdentifier;
    int dest;
    int src;
}serial_header_t;


typedef struct serial_packet {
  serial_header_t header;
  int data[];
} serial_packet_t;

and last one is

typedef struct readAttributePacket
{
     int                     u8SourceEndPointId;
     int                     u8DestinationEndPointId;
     int                     u16ClusterId;
     int                     bDirectionIsServerToClient;
     int                     u8NumberOfAttributesInRequest;
     int                     bIsManufacturerSpecific;
     int                     u16ManufacturerCode;
     int                     pu16AttributeRequestList[];
}readAttributePacket_t;

I am troubling with this code, i just want to cast the data[] array which reside in serial_packet_t into readAttributePacket_t structure. I think the data[] should be

data[]={0x01,0x01,0x04,0x02,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x01};

I need to cast those data to readAttributePacket_t structure. But this below code showing wrong.

void main()
{
int a[]=  {0x32,0x00,0x31,0x69,0x69,0x00,0x00,0x01,0x01,0x04,0x02,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
int i;
readAttributePacket_t *p;
serial_packet_t *data;

data = (serial_packet_t*)&a;

for(i=0;i<20;i++){
    printf(" %02x \n",a[i]);
}
p = (readAttributePacket_t *)&data->data;
printf("\nu8SourceEndPointId:%x \nu8DestinationEndPointId:%x \nu16ClusterId:%04x \nbDirectionIsServerToClient:%x \nu8NumberOfAttributesInRequest:%x \nbIsManufacturerSpecific:%x \nu16ManufacturerCode:%04x",p->u8SourceEndPointId,
    p->u8DestinationEndPointId,
    p->u16ClusterId,
    p->bDirectionIsServerToClient,
    p->u8NumberOfAttributesInRequest,
    p->bIsManufacturerSpecific,
    p->u16ManufacturerCode);


getch();
}

the output should be like

u8SourceEndPointId=01
u8DestinationEndPointId=01
u16ClusterId=0402
bDirectionIsServerToClient=00
u8NumberOfAttributesInRequest=02
bIsManufacturerSpecific=00
u16ManufacturerCode=0000

How could I get the pu16AttributeRequestList[] array into readAttributePacket_t structure, should like that,

pu16AttributeRequestList[0]=0000
pu16AttributeRequestList[1]=0001

Solution

  • You can't just cast an array to a structure because they're simply incompatible types. Due to memory alignment constraints, the compiler needs to insert padding between the fields of a structure, so the members are not located at the memory addresses you may expect. Solutions:

    1. Portable but slower/harder to do manually (preferred): copy manually the fields of the structure to the array.
    2. Shorter to write but GCC-specific: use the __attribute__((packed)) keyword to make GCC not introduce padding between struct fields.