C: byte-copy unsigned char value

c

void callback(const unsigned char* data, int len) {
 unsigned char copydatahere;
}

data is a pointer-to-const situation, that is allocated in library outside. len is a size of data, guessing it to be sizeof(unsigned char)*N.

How I allocate copydatahere to size of len and copy the whole memory behind data including null bytes, string termination chars and anything other that has byte representation? What would be the difference between bcopy and memcpy in this situation?


Addition:
memcpy(pointer+offset, sourcedata, size);that's how you can do 'memcpy append' guys. Thank you all!

Best Answer

Use memcpy. bcopy is only supported on some platforms, and is deprecated in newer standards.

void callback(const unsigned char* data, int len) {
  unsigned char* copydatahere = malloc(len);
  if (!copydatahere) {
    exit(1);
  }
  memcpy(copydatahere, data, len);

  /* ... */

  free(copydatahere);
}
Related Topic