hut123
hut123

Reputation: 455

Problem with about void* and generic function

I am trying to overwrite an element (of unknown size) "copyHere" with newElement, but I am having problems. I set copyHere to the chunk of the elementArr of which I want to overwrite:

void *copyHere = ((char *)elementArray + (i * elementSize));
copyHere = memcpy(copyHere, *newElement, elementSize);

I am getting an error, "invalid use of void expression". How am I misusing the void * or memcpy? From my understanding, I feed in memcpy a destination block of memory and a source block of memory and the size of the source that I want to copy. I have done this. And for void *copyHere, I have traversed through the elementArray (of unknown type, but known size elementSize) to find the block of memory I want to overwrite. Any clues?

Upvotes: 0

Views: 266

Answers (2)

Jim Buck
Jim Buck

Reputation: 20726

If newElement is of type void, you can't have the * to dereference. memcpy needs the pointer anyway.

Upvotes: 1

R.. GitHub STOP HELPING ICE
R.. GitHub STOP HELPING ICE

Reputation: 215257

Remove the * before newElement. You cannot (and do not need to) dereference a void pointer to pass it to memcpy.

Upvotes: 1

Related Questions