Change the line
memcpy(new_container, &buffer[10], 20);
to
memcpy(new_container, (char *)buffer + 10, 20);
That's because &buffer[10]
evaluates to&(*(buffer + 10))
because the array subscript operator has higher precedence than the address of
operator &
. However, buffer
is of type void *
and pointer arithmetic cannot be done on void
pointers because there is no size information. Using the typecast operator (char *)
on buffer
provides the necessary size information so that (char *)buffer + 10
is equivalent to buffer + 10 * sizeof(char)
or the address of the 11
th element in the buffer pointed to by the variable buffer
.