memcpy
Da cppreference.com
![]() |
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
Definido no cabeçalho <string.h>
|
||
void* memcpy( void* dest, const void* src, size_t count ); |
||
Copia
count
bytes da memória apontada por src
à memória apontada por dest
. Se os blocos de memória se sobreposerem, o comportamento é indefinido.Original:
Copies
count
characters from the object pointed to by src
to the object pointed to by dest
. If the objects overlap, the behavior is undefined.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Índice |
[editar] Parâmetros
dest | - | ponteiro para o local de memória de destino, onde será feita a cópia
Original: pointer to the memory location to copy to The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
src | - | ponteiro para o local de memória de origem
Original: pointer to the memory location to copy from The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
count | - | número de bytes que serão copiados
Original: number of bytes to copy The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
[editar] Valor de retorno
dest
[editar] Exemplo
#include <stdio.h> #include <string.h> void main(void) { char destino[150]; char origem[] = "C... uma linguagem que parece simple, mas muito poderosa."; // Copiando todo o string de origem memcpy(destino, origem, strlen(origem)); printf("destino: %s\n", destino); // Uma cópia parcial com somente 4 caracteres memcpy(destino, origem, 4); printf("destino: %s\n", destino); // Na segunda cópia, podemos observar que o string de destino, aparentemente foi // copiado integralmente. Na verdade ele copiou somente 4 bytes. O que aconteceu, // é que o memcpy() não coloca o terminador, o carácter nulo, no final do string // de destino. Na primeira copia acima, nós copiamos o terminador quando informamos // ao memcpy() que queriamos copiar o tamanho total do string, o que inclui o // terminador. // Podemos inserir o terminador, o carácter nulo, manualmente. destino[4] = '\0'; printf("destino: %s\n", destino); // Ou podemos pedir para o memcpy() inserí-lo em uma determinada posição. memcpy(destino+2, "\0", 1); printf("destino: %s\n", destino); // NOTA: No example acima, nós temos que usar " em vez de ' porque o segundo parâmetro // da função memcpy() espera um string e não um carácter. }
Saída:
destino: C... uma linguagem que parece simple, mas muito poderosa. destino: C... uma linguagem que parece simple, mas muito poderosa. destino: C... destino: C.
[editar] Veja também
copia o conteúdo de um bloco de memória para outro Original: moves one buffer to another The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (função) | |
C++ documentation for memcpy
|