Copy array from certain position to another array in c

APP picture APP · Aug 23, 2014 · Viewed 8.1k times · Source

I have array A and i want to copy this array from position x till y to another array in C language. Please help in creating it in c.

Using memcpy copies array from beginning only. I want to copy from particular position to another position.

Answer

Vlad from Moscow picture Vlad from Moscow · Aug 23, 2014
int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int b[5];

memcpy( b, a + 5, 5 * sizeof( int ) );

Also you can do this using an ordinary for loop

for ( int i = 0; i < 5; i++ ) b[i] = a[i+5];