I have a function UartSend() to send data to network through uart. it takes argument unsigned char and an integer
UartSend(unsigned char *psend_data,int length);
i want to send a structure through this function
#pragma pack(push, 1)
struct packet
{
int a;
char b[3];
...
}PacketData;
#pragma pack(pop)
How to covert this structure to unsigned char for sending this data through UartSend? thanks..
You can simply cast to unsigned char *
, provided you get your size correct:
PacketData pkt;
UartSend((unsigned char *)&pkt, sizeof(pkt));
unsigned char *
in this context really just means byte.
Of course, at the other end of the connection, you'll need to incorporate program logic to re-cast to PacketData
(or some similar struct).
EDIT: However, note that as @WhozCraig points out in comments, if your destination platform is not identical to your source platform, you need to consider alignment issues (or, alternatively, rely on a GNU extension like __packed__
)