whats the cleanest way to perform pointer arithmetic in C++? I am trying to add n
bytes to an address.
int *foo;
foo = static_cast<int*> (static_cast<unsigned int>(foo) + sizeof(T))
Later you could recast and use foo differently
char* somestr = reinterpret_cast<char*>(foo)
Is this good enough? Now I understand pointer implementation dosent guarantee all pointers (char*
, int*
) to be implemented of the same size. So not sure if int*
(should it be foo*
) and using unsigned int for math is the cleanest solution. Also, it has to work on both 32 and 64 bit.
Nevermind why Im doing this. Its not all that unusual though. You would run into this issue anytime youre working with a stream (or bytepool) of data that is interpreted dynamically into different types.