Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

189
Visualizações
Deletion Using memcpy in an array

Given an index and an array of integers, I need to delete the element in the given array which was stored in the given index through the use of memcpy(). The new set of elements will be stored on the given array.

Here's an illustration of what I want to do, though I'm having trouble implementing it.

enter image description here

So arrayElem after deleting 10 would look like this:

enter image description here

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

You can't use memcpy, because source and destination overlap, but you can use memmove, e.g.:

memmove(&a[1], &a[2], 5 * sizeof(a[0]));

This copies the 5 elements starting at a[2] down to the 5 elements starting at a[1], taking care of the fact that the source and destination regions overlap.

over 4 years ago · Santiago Trujillo Relatório

0

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 
over 4 years ago · Santiago Trujillo Relatório

0

You caanot do this using memcpy() on the same array. You need to use memmove().

To quote the memcpy() man page

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.

Ref: memmove()

However, if you use a temporary array, then, in 2-steps, you can achieve this using memcpy(), like

  • memcpy() the required part of your given array to the temporary array
  • memcpy() back the temporary array to the given array starting from the required index.

FWIW, neither of the above approach will clear the value in index 6. The existing value will be present. You need to do that manually.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda