deepzero Posted March 15, 2011 Posted March 15, 2011 (edited) hi all, this little problem is killing me: char data[256];mystruct* mystructptr;//copy some stuff into the data buffer int the buffer "data",there are 12 junk bytes, but then there is a mystruct struct. i need to point to that struct... way #1: mystructptr = (mystruct*)(data+12); this works just fine, mystructptr points to the address of "data" + 12. this, however, doesnt work: mystructptr = (mystructptr*)data;mystructptr = (mystruct*)(mystructptr + 12);ormystructptr = (mystructptr + 12)ormystructptr += 12; the data buffer gets increased by ~0x400 bytes, not 12... i really cant see why #2 wouldnt work. Edited March 15, 2011 by deepzero
kao Posted March 15, 2011 Posted March 15, 2011 It's a standard C feature (quote from Kernighan's Programming in C tutorial):If p is a pointer to a structure, any arithmetic on p takes into account the actual size of the structure. For instance, p++ increments p by the correct amount to get the next element of the array of structures. There are several ways to do what you want to, something like this:mystructptr = ((byte *)mystructptr + 12);
deepzero Posted March 15, 2011 Author Posted March 15, 2011 (edited) most awesome,kao! my book didnt (doesnt) mention it... :/ edit: could`ve figured it out myself, though, huh? Edited March 15, 2011 by deepzero
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now