Skip to content

Instantly share code, notes, and snippets.

@BrunoNZ
Last active April 19, 2020 23:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BrunoNZ/97a1393b3cecf2c44cf191cbe39347cc to your computer and use it in GitHub Desktop.
Save BrunoNZ/97a1393b3cecf2c44cf191cbe39347cc to your computer and use it in GitHub Desktop.
Exemplos de usos de ponteiros, em C.
#include <stdlib.h>
#include <stdio.h>
#define T_VETOR 5
#define L_MATRIZ 2
#define C_MATRIZ 2
#define V_BASE 1
#define SEPARADOR "------------------"
// ========================================================================== //
void imprime_valor(int valor, char* tag) {
printf("%s\n", tag);
printf("%d\n", valor);
}
// ========================================================================== //
// ========================================================================== //
int* cria_vetor(int tamanho) {
int* vetor;
vetor = (int*) malloc(sizeof(int)*tamanho);
return vetor;
}
void inicializa_vetor(int* vetor, int tamanho, int valor) {
int x;
for (x=0;x<tamanho;x++) {
vetor[x] = valor;
}
}
void imprime_vetor(int* vetor, int tamanho, char* tag) {
int x;
printf("%s\n", tag);
for (x=0;x<tamanho;x++) {
printf("%d ", vetor[x]);
}
printf("\n");
}
// ========================================================================== //
// ========================================================================== //
int** cria_matriz(int linhas, int colunas) {
int** matriz;
int l;
matriz = (int**) malloc(sizeof(int*)*linhas);
for (l=0;l<linhas;l++) {
matriz[l] = (int*) malloc(sizeof(int)*colunas);
}
return matriz;
}
void inicializa_matriz(int** matriz, int linhas, int colunas, int valor) {
int l,c;
for (l=0;l<linhas;l++) {
for (c=0;c<colunas;c++) {
matriz[l][c] = valor;
}
}
}
void imprime_matriz(int** matriz, int linhas, int colunas, char* tag) {
int l,c;
printf("%s\n", tag);
for (l=0;l<linhas;l++) {
for (c=0;c<colunas;c++) {
printf("%d ", matriz[l][c]);
}
printf("\n");
}
}
// ========================================================================== //
// ========================================================================== //
// CASO 1: Passagem de parametro de um inteiro por valor/parametro
void c1(int v) {
v++;
}
// CASO 2: Passagem de parametro de um inteiro por referencia
void c2(int* v) {
(*v)++;
}
// CASO 3: Passagem de parametro de um ponteiro de inteiros
void c3(int* v, int tamanho) {
int x;
for (x=0;x<tamanho;x++) {
v[x]++;
}
}
// CASO 4: Passagem de parametro de um ponteiro para ponteiros de inteiros
void c4(int** m, int linhas, int colunas) {
int l,c;
for (l=0;l<linhas;l++) {
for (c=0;c<colunas;c++) {
m[l][c]++;
}
}
}
void c5(int* valor) {
if ((*valor) >= 10)
return;
// Para incrementar o valor, usa o ponteiro para usar o valor que esta no endereco apontado;
(*valor)++;
// Para passar para a funcao, usa sem o ponteiro, para usar o endereco do valor;
c2(valor);
}
// ========================================================================== //
int main(int argc, char const *argv[]) {
int valor;
int* vetor;
int** matriz;
int exemplo;
valor = V_BASE;
imprime_valor(valor,"VALOR ORIGINAL");
printf("%s\n", SEPARADOR);
vetor = cria_vetor(T_VETOR);
inicializa_vetor(vetor,T_VETOR,V_BASE);
imprime_vetor(vetor,T_VETOR,"VETOR ORIGINAL");
printf("%s\n", SEPARADOR);
matriz = cria_matriz(L_MATRIZ,C_MATRIZ);
inicializa_matriz(matriz,L_MATRIZ,C_MATRIZ,V_BASE);
imprime_matriz(matriz,L_MATRIZ,C_MATRIZ,"MATRIZ ORIGINAL");
printf("%s\n", SEPARADOR);
exemplo = 1;
// EXEMPLO 1:
printf("EXEMPLO: %d\n", exemplo++);
imprime_valor(valor,"ANTES:");
c1(valor);
imprime_valor(valor,"DEPOIS:");
printf("%s\n", SEPARADOR);
// EXEMPLO 2:
printf("EXEMPLO: %d\n", exemplo++);
imprime_vetor(vetor,T_VETOR,"ANTES:");
c1(vetor[T_VETOR-1]);
imprime_vetor(vetor,T_VETOR,"DEPOIS:");
printf("%s\n", SEPARADOR);
// EXEMPLO 3:
printf("EXEMPLO: %d\n", exemplo++);
imprime_matriz(matriz,L_MATRIZ,C_MATRIZ,"ANTES:");
c1(matriz[L_MATRIZ-1][C_MATRIZ-1]);
imprime_matriz(matriz,L_MATRIZ,C_MATRIZ,"DEPOIS:");
printf("%s\n", SEPARADOR);
// EXEMPLO 4:
printf("EXEMPLO: %d\n", exemplo++);
imprime_valor(valor,"ANTES:");
c2(&(valor));
imprime_valor(valor,"DEPOIS:");
printf("%s\n", SEPARADOR);
// EXEMPLO 5:
printf("EXEMPLO: %d\n", exemplo++);
imprime_vetor(vetor,T_VETOR,"ANTES:");
c2(&(vetor[T_VETOR-1]));
imprime_vetor(vetor,T_VETOR,"DEPOIS:");
printf("%s\n", SEPARADOR);
// EXEMPLO 6:
printf("EXEMPLO: %d\n", exemplo++);
imprime_matriz(matriz,L_MATRIZ,C_MATRIZ,"ANTES:");
c2(&(matriz[L_MATRIZ-1][C_MATRIZ-1]));
imprime_matriz(matriz,L_MATRIZ,C_MATRIZ,"DEPOIS:");
printf("%s\n", SEPARADOR);
// EXEMPLO 7:
printf("EXEMPLO: %d\n", exemplo++);
imprime_valor(valor,"ANTES:");
c3(&(valor),1);
imprime_valor(valor,"DEPOIS:");
printf("%s\n", SEPARADOR);
// EXEMPLO 8:
printf("EXEMPLO: %d\n", exemplo++);
imprime_vetor(vetor,T_VETOR,"ANTES:");
c3(vetor,T_VETOR);
imprime_vetor(vetor,T_VETOR,"DEPOIS:");
printf("%s\n", SEPARADOR);
// EXEMPLO 9:
printf("EXEMPLO: %d\n", exemplo++);
imprime_matriz(matriz,L_MATRIZ,C_MATRIZ,"ANTES:");
c3(matriz[0],C_MATRIZ);
imprime_matriz(matriz,L_MATRIZ,C_MATRIZ,"DEPOIS:");
printf("%s\n", SEPARADOR);
// EXEMPLO 10:
printf("EXEMPLO: %d\n", exemplo++);
imprime_matriz(matriz,L_MATRIZ,C_MATRIZ,"ANTES:");
c4(matriz,L_MATRIZ,C_MATRIZ);
imprime_matriz(matriz,L_MATRIZ,C_MATRIZ,"DEPOIS:");
printf("%s\n", SEPARADOR);
// EXEMPLO 11:
printf("EXEMPLO: %d\n", exemplo++);
imprime_matriz(matriz,L_MATRIZ,C_MATRIZ,"ANTES:");
c5(&(matriz[0][0]));
imprime_matriz(matriz,L_MATRIZ,C_MATRIZ,"DEPOIS:");
printf("%s\n", SEPARADOR);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment