Wednesday, September 28, 2005


During a software design I had to store a huge frame inside variables that were valid data that needed further processing . Instead of using a simple solution, which will be given next, I declared a bunch of variables to accomplish it. Suppose you have a frame:

void processFrame( unsigned char * uFrame )
/* copy it to several variables here */
memcpy( &myvar[0], &uFrame[0], 12 );

The above code will save the 12 bytes in a myvar variable. Suppose you have a huge list of bytes, instead of copying each one of them to variables, simply declare a struct to store everything.

struct MyStruct{
unsigned char a[12];
unsigned char b[2];
/* and it may go further*/
} myStructReal;

Next, just point to the first address of the structure and sum up the total of bytes:

memcpy( &myStructReal, &fFrame[0], totalofbytes);

Ok, you will have to be sure of how many bytes will come inside the data frame, but generally you know it. It is a major ellegant and simpler solution. As, C treats structures as a series of data addresses, it will fit your huge array of bytes into a struct, piece of cake right?

No comments: