strncat
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>
|
||
char *strncat( char *dest, const char *src, size_t count ); |
||
Acrescenta uma seqüência de bytes apontado por
src
para uma seqüência de bytes apontado por dest
. Na maioria dos personagens count
são copiados. A seqüência de bytes resultante é nula terminada. Se as cordas se sobrepõem, o comportamento é indefinido. Original:
Appends a byte string pointed to by
src
to a byte string pointed to by dest
. At most count
characters are copied. The resulting byte string is null-terminated. If the strings 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 byte string terminada em nulo para acrescentar
Original: pointer to the null-terminated byte string to append 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 byte string terminada em nulo para copiar
Original: pointer to the null-terminated byte string 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 máximo de caracteres a serem copiados
Original: maximum number of characters 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 test1() { char origem[] = "ABCDEF"; char origem2[] = "Este string contém 32 caracteres"; char destino[10] = ""; char destino2[10] = ""; strncpy(destino, "123", 3); printf("destino: '%s', tamanho: %lu\n", destino, strlen(destino)); strncat(destino, "-", 1); printf("destino: '%s', tamanho: %lu\n", destino, strlen(destino)); strncat(destino, origem, 4); printf("destino: '%s', tamanho: %lu\n", destino, strlen(destino)); // reseta o string destino copiando o carácter nulo para a posição 0 do string. destino[0] = '\0'; // Normalmente quando se quer copiar um string inteiro, se usa a função strlen() // para auxiliá-lo, retornando o tamanho do string de origem. É sua responsabilidade // garantir que o string de destino seja grande o suficiente para acomodar o string // que está sendo copiado. Se o string de destino (onde está sendo feita a cópia) // tentar guardar um string maior do que o seu tamanho, ele será corrompido e o // resultado é indefinido. Lembre-se de que você deve reservar 1 carácter para o // terminador do string, o carácter nulo '\0'. strncat(destino, origem, strlen(origem)); printf("destino: '%s', tamanho: %lu\n", destino, strlen(destino)); // ** Perigoso **: Não faça isso. O string de destino (destino2) tem somente 10 bytes // e o strncat() irá copiar 32 bytes provenientes do string (origem2), o que // logicamente não irá caber em 10 bytes. Este código irá compilar mas o resultado da // cópia em tempo de execução é indefinido. // strncat(destino2, origem2, strlen(origem2)); }
Saída:
destino: '123', tamanho: 3 destino: '123-', tamanho: 4 destino: '123-ABCD', tamanho: 8 destino: 'ABCDEF', tamanho: 6
[editar] Veja também
concatena duas strings Original: concatenates two strings The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (função) | |
copia uma string para outra Original: copies one string 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 strncat
|