I want to use memcpy to copy from a certain byte in c

63 Views Asked by At

I want to copy some part of data into source_mac

This is my code:

uint8_t *data
uint8_t *source_mac

memcpy(&source_mac, &data[6], 6);

So I want to copy 6 bytes from data, starting with the 6th byte in data, into source_mac... What am I doing wrong?

1

There are 1 best solutions below

2
chqrlie On

There are multiple problems in your code fragment:

  • the 6th byte is data[5]
  • you pass &source_mac as the destination, which is the address of the source_mac pointer, not the address of the destination array. You should either pass &source_mac[0] or simply source_mac

Here is a modified version:

void my_function(uint8_t *data, uint8_t *source_mac) {
    [...]
    memcpy(source_mac, &data[5], 6);
    [...]