Jump to content
  • Advertisement

Archived

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

JoCCo

C++, getting data from a unknown structure

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 code

float 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 this post


Link to post
Share on other sites
Guest Anonymous Poster
Make sure that arrayOfData is a (void*). Pointer arithmetic works differently depending on the type of pointer.

Share this post


Link to post
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 this post


Link to post
Share on other sites
Guest Anonymous Poster
oh, NM, it''s pointing to some kind of byte so that should work ok.

Share this post


Link to post
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 before


UInt 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 this post


Link to post
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 this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!