domingo, 26 de outubro de 2014

Memcpy - Copiando uma Matriz de inteiros


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 inteiros 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 alguns problemas de execução.

Veja abaixo imagens do programa em execução:



Veja abaixo o código do programa:



#include <stdio.h>
#include <conio.h>
#define tam 10
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 INTEIROS" );
    int i;
    do {
         Janela5 ( );
         textcolor ( LIGHTRED );
         gotoxy ( 24, 7 );
         printf ( "MEMCPY - COPIANDO UMA MATRIZ DE INTEIROS" );
         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 ( );
    int Vet [ tam ] [ tam ] =
            {{36, 48, 34, 89, 25, 56, 1, 59, 12, 81 },
             { 15, 22, 65, 17, 91, 6, 11, 93, 26, 54 },
             { 85, 8, 35, 24, 39, 27, 76, 14, 21, 99 },
             { 66, 95, 87, 69, 100, 51, 20, 18, 71, 46 },
             { 33, 45, 57, 67, 80, 50, 4, 82, 83, 92 },
             { 31, 13, 94, 16, 47, 10, 61, 63, 74, 60 },
             { 5, 88, 37, 77, 23, 38, 86, 43, 72, 78 },
             { 70, 98, 73, 52, 42, 28, 68, 3, 41, 75 },
             { 58, 79, 84, 97, 64, 55, 9, 19, 7, 32 },
             { 44, 62, 90, 53, 40, 30, 96, 29, 2, 49 }};
    int matriz [ tam ] [ tam ];
    int i, j;
    gotoxy ( 27, 3 );
    system ( "cls" );
    Janela5 ( );
    textcolor ( LIGHTRED );
    gotoxy ( 24, 3 );
    printf ( "MEMCPY - COPIANDO UMA MATRIZ DE INTEIROS" );
    textcolor ( LIGHTBLUE );
    gotoxy ( 29, 5 );
    printf ( "IMPRIMINDO A MATRIZ ORIGINAL" );
    textcolor ( YELLOW );
    for ( i = 0; i < tam; i++ ) {
         gotoxy ( 18, i + 8 );
         for ( j = 0; j < tam; j++ ) {
             printf ( " %3d ", Vet [ i ] [ j ] );
         }
    }
    Sleep ( 1800 );
    textcolor ( LIGHTGREEN );
    gotoxy ( 32, 22 );
    printf ( "PRESSIONE QUALQUER TECLA" );
    getche ( );
    memcpy ( *matriz, Vet, sizeof( *matriz ) * 10 );
    system ( "cls" );
    Janela5 ( );
    textcolor ( LIGHTRED );
    gotoxy ( 24, 3 );
    printf ( "MEMCPY - COPIANDO UMA MATRIZ DE INTEIROS" );
    textcolor ( LIGHTBLUE );
    gotoxy ( 29, 5 );
    printf ( "IMPRIMINDO A MATRIZ COPIADA" );
    textcolor ( LIGHTGREEN );
    printf ( "\n\n" );
    for ( i = 0; i < tam; i++ ) {
         gotoxy ( 18, i + 8 );
         for ( j = 0; j < tam; j++ ) {
             printf ( " %3d ", matriz [ i ] [ j ] );
         }
    }
    Sleep ( 1800 );
    textcolor ( LIGHTRED );
    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.