O C possui funções para manipulação de memória, e a função memcpy(); é uma delas, blocos de memória podem ser copiados facilmente com esta função, cuja sintaxe é: memcpy ( *espaço1, *espaço2, bytes );
a função memcpy(); faz parte do arquivo de cabeçalho string.h.
E neste código mostro um exemplo de como copiar uma Matriz de string completa para uma outra Matriz vazia previamente declarada.
Fica esclarecido que a área de memória do destino, jamais pode ser menor que o de origem, pra que evite aguns problemas de execução.
Veja abaixo imagens do programa em execução:
#include <stdio.h>
#include <conio.h>
void Janela5 ( ) {
system ( "Color F0" );
int lin, col;
for ( lin = 0; lin <= 25 ; lin++ ) {
for ( col = 0; col <= 80 ; col++ ) {
gotoxy ( col , lin );
if ( lin == 2 ) {
textbackground ( LIGHTBLUE );
printf ( " " );
}
if ( col == 1 ) {
textbackground ( LIGHTBLUE );
printf ( " " );
}
if ( lin == 25 ) {
textbackground ( LIGHTBLUE );
}
if ( col == 80 ) {
textbackground ( LIGHTBLUE );
printf ( " " );
}
}
}
textbackground ( BLACK );
}
void inicio ( ) {
system ( "title MEMCPY
- COPIANDO UMA MATRIZ DE STRIMG" );
int i;
do {
Janela5 ( );
textcolor ( LIGHTRED );
gotoxy ( 24 , 7 );
printf ( "MEMCPY -
COPIANDO UMA MATRIZ DE STRIMG" );
textcolor ( YELLOW );
gotoxy ( 25 , 10 );
printf ( "Programa
desenvolvido por:" );
textcolor ( LIGHTCYAN );
gotoxy ( 52 , 10 );
printf ( "Samuel Lima" );
textcolor ( LIGHTGREEN );
gotoxy ( 34 , 12 );
printf ( "sa_sp10@hotmail.com" );
textcolor ( LIGHTBLUE );
gotoxy ( 24 , 14 );
printf ( "DIGITE PARA SAIR OU PARA CONTINUAR " );
textcolor ( LIGHTRED );
gotoxy ( 32 , 14 );
printf ( "1" );
textcolor ( LIGHTRED );
gotoxy ( 47 , 14 );
printf ( "2" );
gotoxy ( 41 , 16 );
scanf ( "%d" , &i );
fflush ( stdin );
if ( i == 1 ) {
textcolor ( LIGHTRED );
gotoxy ( 35 , 20 );
printf ( "MUITO
OBRIGADO" );
Sleep ( 1800 );
exit ( 0 );
}
if ( i == 2 ) {
system ( "cls" );
return;
} else {
textcolor ( LIGHTRED );
gotoxy ( 37 , 20 );
printf ( "\aOPÇÃO
ERRADA" );
Sleep ( 1800 );
system ( "cls" );
}
} while ( i );
}
int main ( ) {
inicio ( );
Janela5 ( );
int i;
char Frutas [ 6 ] [ 19 ] =
{ "Mulher
Melancia", "Mulher melão", "Mulher Moranguinho",
"Mulher Pêra", "Mulher
Jaca", "Mulher Banana" };
char Mulh_Frut [ 6 ] [ 19 ];
textcolor ( LIGHTRED );
gotoxy ( 24 , 3 );
printf ( "MEMCPY -
COPIANDO UMA MATRIZ DE STRIMG" );
textcolor ( LIGHTBLUE );
gotoxy ( 10 , 5 );
printf ( "IMPRIMINDO A
MATRIZ ORIGINAL" );
textcolor ( YELLOW );
for ( i = 0; i < 6 ; i++ ) {
gotoxy ( 15 , i + 7 );
printf ( "%s\n" , Frutas [ i ] );
}
Sleep ( 800 );
textcolor ( LIGHTRED );
gotoxy ( 5 , 15 );
printf ( "\aBANANA ?? OPS!! TOH FORA MANO" );
getche ( );
memcpy ( Mulh_Frut , Frutas , sizeof ( Mulh_Frut ) );
textcolor ( LIGHTBLUE );
gotoxy ( 49 , 5 );
printf ( "IMPRIMINDO A
MATRIZ COPIADA" );
textcolor ( YELLOW );
for ( i = 0; i < 6 ; i++ ) {
gotoxy ( 55 , i + 7 );
printf ( "%s\n" , Mulh_Frut [ i ] );
}
Sleep ( 800 );
textcolor ( LIGHTRED );
gotoxy ( 47 , 15 );
printf ( "\aBANANA ??
OPS!! TOH FORA MANO" );
Sleep ( 1800 );
textcolor ( LIGHTGRAY );
gotoxy ( 35 , 22 );
printf ( "MUITO
OBRIGADO" );
getche ( );
exit ( 0 );
}
Nenhum comentário:
Postar um comentário
Observação: somente um membro deste blog pode postar um comentário.