Michał Ziobro Michał Ziobro - 26 days ago 5x
C Question

C: unsigned char * bytes array copying elements to other unsigned char * array

I think that copying elements of array like this:

unsigned char *new_bytes_array = malloc(sizeof(unsigned char)*length/2);
for(int i=0, j=0; i<length; i++) {
if(i % 2 == 0) continue;
new_bytes_array[j] = old_bytes_array[i];


is making copy but value not by reference, but I would like to make sure that this will be deep copy not just shallow copy of references or pointers.

I know that this is maybe easy and ridiculous qustion, but I cannot find similar on stack rather memcpy() all array, but I want to copy only some elements for example every second element, or copy 1-3 elements and skip 4th element, (if i%4 == 0 skip element).

alk alk

new_bytes_array[j] evaluates to an unsigned char.

Assuming old_bytes_array[i] does as well, then this

new_bytes_array[j] = old_bytes_array[i];

copies an unsigned char and not a pointer, not an unsigned char*.