Shantanu Banerjee
Shantanu Banerjee

Reputation: 1447

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

Upvotes: 3

Views: 12375

Answers (3)

omer sakkar
omer sakkar

Reputation: 1

I think the only thing that you need to do in to remove the address operator from the casting statement.

data = (serial_packet_t*)a;

instead of

data = (serial_packet_t*)&a;

as far as I know, everything should work fine from here.

Upvotes: 0

huseyin tugrul buyukisik
huseyin tugrul buyukisik

Reputation: 11920

Construct a union of 3 structs. all on equal memory space. then you dont even need to cast.

Upvotes: 0

user529758
user529758

Reputation:

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.

Upvotes: 7

Related Questions