I have to create a generic array that can contain generic data structures. How can i put a generic structure into an empty slot of my void array?
This is my code.
struct CircularBuffer {
int E;
int S;
int length; // total number of item allowable in the buffer
int sizeOfType; // size of each element in the buffer
void *buffer;
};
struct CircularBuffer* circularBufferInit(int length, int sizeOfType) {
struct CircularBuffer *cb = malloc(sizeof(struct CircularBuffer));
cb->E = 0;
cb->S = 0;
cb->length = length;
cb->sizeOfType = sizeOfType;
cb->buffer = malloc(sizeOfType *length);
return cb;
}
int circularBufferIsEmpty(struct CircularBuffer* cb) {
if (cb->S == cb->E)
return 1; //empty
else
return 0;
}
int circularBufferIsFull(struct CircularBuffer *cb) {
int nE = (cb->E + 1) % (cb->length);
if (nE == cb->S)
return 1; //full
else
return 0;
}
void circularBufferAdd(struct CircularBuffer *cb, void* obj) {
memcpy(cb->buffer + cb->E, obj, cb->sizeOfType);
}
[...]
memcpy is the problem...