0
for (i = 0; i < n1; i++) 
   L[i] = arr[l + i]; 

Because I want to copy a large array,I heard that need to use memcpy.

3
  • You can get a pointer to an arbitrary element at index l by using the address-of operator, as in &arr[l]. This could be used as the source argument for a memcpy call. Commented Jun 17, 2020 at 6:07
  • 4
    Does this answer your question? memcpy with startIndex? Commented Jun 17, 2020 at 6:09
  • Adam's answer is of better quality. Have a look at it. Commented Jun 17, 2020 at 6:17

2 Answers 2

4

memcpy as the name says, copy memory area. is a C standard function under string.h.

void *memcpy(void *dest, const void *src, size_t n); 

description:

The memcpy() function copies n bytes from memory area src to memory area dest. The memory areas must not overlap. Use memmove(3) if the memory areas do overlap. The memcpy() function returns a pointer todest. for more details goto man7: memcpy

so in your case the call would be:

memcpy(L, &arr[l], n1*sizeof(arr[l]));

sizeof one array elementis:

sizeof(arr[l])

make sure that (l+n1) doesnot exceeds the array boundaries! its you responsibility.

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

1 Comment

@曾品翔, please accept my answer (by pushing the beside V ) if you find it useful. thanks.
1
memcopy(destination, source, length)

That would be in your case:

int len = sizeof(int) * n1;
memcopy(L, arr+l, len);

Note: You may have to fix the length calculation according to the type you are using. Moreover, you should also remember to add 1 to include the \0 character that terminates char arrays if you are dealing with strings.

2 Comments

If you use sizeof (arr[0]) there is no more need to adjust according to type. And it's actually memcpy.
@Gerhardh yep, that's why I advised the OP to go for Adam's answer. I did not want to plagiarize his answer.

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.