Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++, getting data from a unknown structure

Started by JoCCo Jan 13, 2004 at 4:26 PM 8 replies 600+ views
Original Post
JoCCo
JoCCo
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...
Zipster
Zipster
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));
JoCCo
JoCCo
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]
sphinx23
sphinx23

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.
JoCCo
JoCCo
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]
JoCCo
JoCCo
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...
Zipster
Zipster
quote:
Is this how you meant ?

Yeah. What you have is what I mean. I''m such a deusche :/
Nytegard
Nytegard
Although someone already gave a good answer, I would think in this situation, you might want to consider using a union.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.