I’d like to implement something like below:
struct MyArray {
void* Elements;
int Capacity;
int ElementsCount;
size_t ElementSize;
//methods
void AddElement(void* item);
//...
};
void* Elements should be pointer to items of any type. Every element should have particular size (ElementSize variable) and AddElement(void*) method should add item to existing array.
The problem is that I can’t do pointer arythmetic with my Array, I know I need to use casting every time I want to use it but I completely don’t know how to do it.
And I know template would be better solution for it, but in this case I’d like to practise with pointers 🙂
Thanks for help in advance.
Yes, you cannot do pointer arithmetic on void*, you will have to cast to char* to do the arithmetic e.g. something like:
Note that you need
static_castto cast from void* to char* and you do not need to explicity cast at all back to void* which is why I can assign it to insertionPoint.