Created
April 7, 2019 21:08
-
-
Save santiagosilas/ba0e03fd2244bc5bfaf7cd7db8407658 to your computer and use it in GitHub Desktop.
ANSI C - Alocação Dinâmica de Vetor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #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