1

I have two arrays, a and b, both of length x bytes. I need to use memcpy to copy the memory from a to b. What is the correct syntax to use, since memcpy takes in two void* as its argument? Is it:

memcpy(&a[0], &b[0], x);
2
  • You mean from b to a? Commented Oct 15, 2015 at 2:43
  • in memcpy() the first argument is the address of the first byte of the destination. the second argument is the address of the first byte of the source. the third argument is the number of bytes to copy. Note: your posted code copies from 'b' to 'a' while that actual question is saying to copy from 'a' to 'b' Commented Oct 15, 2015 at 5:30

1 Answer 1

4

To copy x bytes from a to b, you'd say:

memcpy(b, a, x);

memcpy() takes the destination first, then the source.

Array names evaluate to the array address when used as parameters, and void * parameters will accept any pointer you throw at them.

memcpy(&b[0], &a[0], x);

would be equivalent, but needlessly verbose.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.