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.

Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
曾品翔
  • 13
  • 1
  • 6
  • 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. – Some programmer dude Jun 17 '20 at 06:07
  • 4
    Does this answer your question? [memcpy with startIndex?](https://stackoverflow.com/questions/1163624/memcpy-with-startindex) – Chase Jun 17 '20 at 06:09
  • Adam's answer is of better quality. Have a look at it. – Tarik Jun 17 '20 at 06:17

2 Answers2

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.

Adam
  • 2,820
  • 1
  • 13
  • 33
  • 1
    @曾品翔, please accept my answer (by pushing the beside V ) if you find it useful. thanks. – Adam Jun 17 '20 at 09:17
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.

Tarik
  • 10,810
  • 2
  • 26
  • 40
  • If you use `sizeof (arr[0])` there is no more need to adjust according to type. And it's actually `memcpy`. – Gerhardh Jun 17 '20 at 07:49
  • @Gerhardh yep, that's why I advised the OP to go for Adam's answer. I did not want to plagiarize his answer. – Tarik Jun 17 '20 at 09:15