Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Arithmetic with void pointers in C++

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

like image 680
Tihomir Mitkov Avatar asked Dec 21 '22 17:12

Tihomir Mitkov


1 Answers

You can cast the pointer to char* (+1 on such pointer is offset by one byte) or any other pointer type if that suits your needs better.

However, this approach is grossly error prone! You better check your design, something smells here! void* are in 99% of cases unnecessary in C++, designs that use them are usually more "C" than "C++". Remember, templates and inheritance should be the way to do these things.

like image 148
Matěj Zábský Avatar answered Jan 08 '23 10:01

Matěj Zábský