4

I'm looking for a C function similar to the C# Array.Copy(). I found memcpy() only, but I need to copy from specific index too.

3 Answers 3

7

If you have an array like this:

SomeType myArray[50];

And you want to copy elements indexed 19-29 (the 20th through 30th elements).

Then you do:

memcpy(dest, &myArray[19], 10 * sizeof(SomeType));

Note: this code-segment makes no provision for initializing myArray, or allocating memory to dest

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

Comments

3

memcpy is all you have. If you want to copy specific ranges, it's something like this:

memcpy(dst, &src[i_start], num_to_copy * sizeof(*src));

Comments

1

Use pointer arithmetic. It's evil, but in C, it's your friend.

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.