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?

[Webinar] Streamline your web hosting managementRegister Today

x
 
pgnatyukConnect With a Mentor Commented:
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
 
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
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
 
jkrConnect With a Mentor Commented:
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
 
SuperdaveConnect With a Mentor Commented:
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
All Courses

From novice to tech pro — start learning today.