Skip to content

Instantly share code, notes, and snippets.

@santiagosilas
Created April 7, 2019 21:08
Show Gist options
  • Select an option

  • Save santiagosilas/ba0e03fd2244bc5bfaf7cd7db8407658 to your computer and use it in GitHub Desktop.

Select an option

Save santiagosilas/ba0e03fd2244bc5bfaf7cd7db8407658 to your computer and use it in GitHub Desktop.
ANSI C - Alocação Dinâmica de Vetor
#include <stdio.h>
#include<stdlib.h>
#include<time.h>
int *CriarVetor(int n){
int i;
// Cria um vetor dinamicamente alocado
int *v = malloc(n * sizeof(int));
// Inicializa o vetor
for(i=0;i<n;i++)
*(v+i) = rand() % 10;
return v;
}
void ImprimirVetor(int *v, int n){
int i;
for(i=0;i<n;i++)
printf("%d\t", *(v+i));
printf("\n");
}
int main(void) {
// Altera o seed
srand(time(NULL));
int *vetor = CriarVetor(5);
ImprimirVetor(vetor, 5);
free(vetor);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment