Arithmetic with void pointers in C ++

I need to access an object in a buffer pointed to by a pointer to a void. The object is located with a specific offset, but since arithmetic on the void pointer is prohibited, how can I access the object?

+4
source share
1 answer

You can cast a pointer to char* (+1 on such a pointer is shifted by one byte) or any other type of pointer if it suits you best.

However, this approach is highly error prone! Better check your design, something smells here! void* in 99% of cases are not needed in C ++, projects that use them are usually more "C" than "C ++". Remember that templates and inheritance should be the way to do this.

+10
source

Source: https://habr.com/ru/post/1340543/


All Articles