BYTE datatype used for an unknown amount of data

A

aengus

Hi,

I am looking to find out how to deal with the BYTE datatype in VC, as
used below:

This struct is part of the API for a Garmin GPS unit:

typedef struct
{
unsigned char mPacketType; // 0 1 byte
unsigned char mReserved1; // 1 1 byte
unsigned short mReserved2; // 2,3 2 bytes
unsigned short mPacketId; // 4,5 2 bytes
unsigned short mReserved3; // 6,7 2 bytes
unsigned long mDataSize; // 8-11 4 bytes
BYTE mData[1]; // 12+ x bytes
} Packet_t;

The last element mData holds a variable amount of data the size of
which is stored in mDataSize. I am unsure how to deal with it.

In one case, I need to store an unsigned short in it, so I have
written:

mDataSize = sizeof(unsigned short);
mData[0] = 5;

But this does not seem to work... can anyone help?

(Visual Studio .NET 2002, Intel Centrino)

thanks,

Aengus.
 
M

Marcus Heege

Hi Aengus
Hi,

I am looking to find out how to deal with the BYTE datatype in VC, as
used below:

This struct is part of the API for a Garmin GPS unit:

typedef struct
{
unsigned char mPacketType; // 0 1 byte
unsigned char mReserved1; // 1 1 byte
unsigned short mReserved2; // 2,3 2 bytes
unsigned short mPacketId; // 4,5 2 bytes
unsigned short mReserved3; // 6,7 2 bytes
unsigned long mDataSize; // 8-11 4 bytes
BYTE mData[1]; // 12+ x bytes
} Packet_t;

The last element mData holds a variable amount of data the size of
which is stored in mDataSize. I am unsure how to deal with it.

In one case, I need to store an unsigned short in it, so I have
written:

mDataSize = sizeof(unsigned short);
mData[0] = 5;

But this does not seem to work... can anyone help?

(Visual Studio .NET 2002, Intel Centrino)

thanks,

Aengus.

Your code would cast the int value 5 to a BYTE and assign it to mData.

Try this:

Packet_t* p = (Packet_t*)AllocSomeHow(sizeof(Packet_t) - sizeof(BYTE) +
sizeof(int);
*((int*)(&p->mData)) = 5;
 
A

aengus

Thanks Marcus,

That seems to work perfectly. It was an unsigned short, rather than an
int, so the code now reads:

Packet_t* p = (Packet_t*)malloc(sizeof(Packet_t) - sizeof(BYTE) +
sizeof(unsigned short));

*((unsigned short*)(&p->mData)) = (unsigned short)5;

Now to understand that...
 
Top