You may not use function memcpy
because the ranges of the array overlap each other. In this case the behaviour will be undefined.
Instead you have to use standard function memmove
.
Here is a demonstrative program
#include <stdio.h>
#include <string.h>
size_t remove_by_index( int a[], size_t n, size_t i )
{
if ( i < n )
{
memmove( a + i, a + i + 1, ( n - i - 1 ) * sizeof( *a ) );
--n;
}
return n;
}
int main( void )
{
int a[] = { 1, 10, 5, 8, 4, 51, 2 };
const size_t N = sizeof( a ) / sizeof( *a );
size_t n = N;
for ( size_t i = 0; i < n; i++ ) printf( "%d ", a[i] );
printf( "\n" );
n = remove_by_index( a, n, 1 );
for ( size_t i = 0; i < n; i++ ) printf( "%d ", a[i] );
printf( "\n" );
return 0;
}
The program output is
1 10 5 8 4 51 2
1 5 8 4 51 2