Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / October 2005

Tip: Looking for answers? Try searching our database.

BYTE datatype used for an unknown amount of data

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
aengus@gmail.com - 26 Oct 2005 09:23 GMT
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.
Marcus Heege - 26 Oct 2005 20:56 GMT
Hi Aengus
> Hi,
>
[quoted text clipped - 30 lines]
>
> 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;
aengus - 27 Oct 2005 05:59 GMT
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...

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.