#### Archived

This topic is now archived and is closed to further replies.

# C++, getting data from a unknown structure

This topic is 5156 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello, I have a bit of a problem when I''m developing a app, I have a function that I want to be as flexiable as possible, to this end I have designed the following system: function(slsByte* arrayOfData,slsUInt theDefinedStructure) using the values of theDefinedStructure Integer and a few rules in how you specfiy the format of a arrayOfData structure I can figure out what datatypes are in the structure and in what order. My question is, how do I access this data without type casting to the structure in question(as I said I want the function to be flexible and therefor I don''t have a defined structure). If you don''t understand what I mean, or you want something more explained, please post, this problem has been giving my headaches for quite some time now...

##### Share on other sites
You can use char pointers to the offset in the structure. Eventually though you have to cast it to some type to work with it. So if you know the byte offset, just do:

int integer_var = *((int&)(arrayofData + offset));float float_var = *((float&)(arrayofData + other_offset));

##### Share on other sites
The code you wrote issued a syntax error (& is a l-hand value)
so I figured you meant * and it still dosen't give me the correct value...

Is this how you meant ?

//arrayOfData contains this structure:struct name{float[3] XYZ;float[2] uv;}// and this is my codefloat temp;temp = *((float*)(arrayOfData + sizeof(float)));

[edited by - JoCCo on January 13, 2004 6:10:42 PM]

[edited by - JoCCo on January 13, 2004 6:20:31 PM]

##### Share on other sites
Make sure that arrayOfData is a (void*). Pointer arithmetic works differently depending on the type of pointer.

##### Share on other sites
struct MyStruct{ long LongValue; float FloatValue;};function(byte* data){ long LongValue = (long *)(data) float FloatValue = (float *)(data + 1*sizeof(long));}

It''s all from my memory, i haven''t testet it. but i think that is somehow what you want.

##### Share on other sites
oh, NM, it''s pointing to some kind of byte so that should work ok.

##### Share on other sites
I've tried using an offset (since I figured it ought to be one) this is the code I used :
// same structure as beforeUInt  offset=0;float temp=0.0f;//(in a loop)temp = *((float*)(arrayOfData + offset));offset +=sizeof(float);

Edit : And Obviosly it didn't work, temp gets alot of values, not the correct ones though :-(

[edited by - JoCCo on January 13, 2004 6:26:50 PM]

##### Share on other sites
I''m terribly sorry to have bothered you all, the code I posted last (the code I wrote several hours ago) did actually work...

That just goes to prove that you never should code more then a few hours in a row...

Thank you to everyone that read / posted in the topic...

##### Share on other sites
quote:
Is this how you meant ?

Yeah. What you have is what I mean. I''m such a deusche :/

##### Share on other sites
Although someone already gave a good answer, I would think in this situation, you might want to consider using a union.