Inreasing (void*) by Int

Hello ;),
I have a problem with increasing pointers in C++.

I've tried to write a inc procedure similar to the one in Delphi, but it didn't workout with output.

Code:
1
2
3
4
5
6
7
8
9
10
void inc(__out int x, int y)
{
	for (signed i=1; i<=y; i++)
	{
		x++;
	}
}

void* somePointer = (void*)(0x00000123);
inc((int)imageBase, 12);


I hope someone can help me correcting my mistake.
The function definitely work, but the var somePointer is not changed
I'm not sure why somePointer would be changed given your code snippit there...

Have you considered using a template function?
1
2
3
4
5
6
template <typename T>
T inc( T& t, int incr = 1 )
{
    t += incr;
    return t;
}

One thing that this function does is it takes into account the type of the pointer. So if you call it with a pointer to a 16-bit value, the address is incremented in two byte multiples. If you call it with a pointer to an 8-bit value, the address is incremented in one byte multiples. For regular integers it gets incremented normally...

One thing that has always bothered me about Pascal's 'inc' procedure is that it isn't a function. It ticks me off that you have to choose 'inc' or 'succ', but not both... So my version does both... :-) (If you don't like that, just fix it to return void.)

Hope this helps.

[EDIT] You know, after all this, do you realize that you can do this directly in C++?
unsigned char value = *(somePointer += 12);
Last edited on
Topic archived. No new replies allowed.