Jump to content

increasing pointers


deepzero

Recommended Posts

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);
or
mystructptr = (mystructptr + 12)
or
mystructptr += 12;

the data buffer gets increased by ~0x400 bytes, not 12...

i really cant see why #2 wouldnt work. :(

Edited by deepzero
Link to comment

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);
Link to comment

most awesome,kao!

my book didnt (doesnt) mention it... :/

edit:

could`ve figured it out myself, though, huh? :)

Edited by deepzero
Link to comment

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...