How to parse or move buffer data into a structure?

struct ParsedStruct
{
   int index;   /* one byte index */
   float val;   /* 4 byte value */
};

ParsedStruct parsed_data[16];

Some function passes buffer of size 80 bytes and address of first member of structure ParsedStruct to func2().
The data format in the buffer is as follows:
one byte index followed by 4 byte float value.  This repeats Sixteen times.

func2( void * buff, ParsedStruct *parsed)
{
  // Implement this function so data is moved from buffer to passed array of structures
 //  Please fix if there are any mistakes in arguments passed to this function
}
LVL 1
naseeamAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

jkrCommented:
Since this is C/C++ - cast it! ;o)

E.g.
ParsedStruct* func2( void * buff)
{
  return reinterpret_cast<ParsedStruct*>(buff);
  // or: return (ParsedStruct*) buff;
}

void* buff;// fill it somehow

for (int i = 0; i < 16; ++i) {

  ParsedStruct* p = (void*)((char*) buff + i * sizeof(ParsedStruct));
}

Open in new window

0
williamcampbellCommented:
Homework?
0
naseeamAuthor Commented:
This is not homework.  I need to put buffer data into array of structure.  

jkr:   I didn't understand.  Can you show completed code.  Yes, this is C++
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

pgnatyukCommented:
Probably I didn't understand
struct ParsedStruct
{
   int index;   /* one byte index */
   float val;   /* 4 byte value */
};

int index - the size is 4 bytes. float val - the size is 4 bytes.
The structure size is 8. 8 x 16 = 96.

You need to parse an input buffer.
ParsedStruct parsed_data[16];
unsigned char* p = (unsigned char*)buffer;
for (int i=0l i < 16; i++)
{
     parsed_data[i].index = *p;
     p++;
     parsed_data[i].val = *(float*)p;
     p += 4;
}

Try this.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jkrCommented:
This is pretty much the complete code (except filling 'buff'). The basic idea behind casting is to tell the compiler to interpret a certain memory region ('buff' in your case) as something different. To make that a bit simpler, let's have a look at that without the loop:
void* buff;// fill it somehow

// Tell the compiler that 'p' is an interpretation of 'buff' as 'ParsedsStruct'
ParsedStruct* p = reinterpret_cast<ParsedStruct*>(buff);

// now, you can use 'p' as a pointer to 'ParsedBuf', i.e.
std::cout << "Index: " << p->index << " Value: " << p->val << std::endl;

Open in new window

0
jkrCommented:
BTW, for more details, see http://www.cplusplus.com/doc/tutorial/typecasting/ ("Type Casting")
0
pgnatyukCommented:
@jkr: in ParsedStruct the filed index is int, in the input buffer it is one byte.
0
SuperdaveCommented:
I like pgnatyuk's solution, except if you want it to be portable beyond 80x386(-64),
you should replace

parsed_data[i].val = *(float*)p;

with

memcpy(&parsed_data[i].val,p,sizeof(float));

Not all processors like referencing floating point at any old address.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.