• Advertisement
Sign in to follow this  

Struct member offsets

This topic is 2123 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

Hi

Say you have a char pointer "p" to an array of characters. Now p+1 would give you the second character and so on.

The same applies for structs. If a struct is 40 Bytes long, then structobject+1 increments by 40 bytes.

How does this applies to members of a struct?

[CODE]
struct MyStruct
{
int no;
int idx_x,idx_y,idx_z;
float tx,ty,tz,tw;
};


MyStruct &pData = ArrayOfMyStructs[47];
int offset = 3;
pData.no+offset; //Where is this address?
[/CODE]

Does pData.no+offset; refer to ArrayOfMyStructs[50].no, or does it refer to ArrayOfMyStructs[47].idx_z?

Share this post


Link to post
Share on other sites
Advertisement
When [i]pData[/i] is a pointer, then [i]pData.no[/i] is not valid syntax to begin with. But even if you meant [i]pData->no[/i], then [i]pData->no+3[/i] simply means the value of the [i]no[/i]-member plus three.

If, on the other hand, you meant [i](&pData->no)+3[/i], then that is equivalent to [i]int *pNo=(&pData->no); pNo+3[/i] which moves an [i]int[/i]-pointer three steps forwards. In practice, that would mean it likely points to [i]ArrayOfMyStructs[47].idx_z[/i].

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement