Skip to content

Instantly share code, notes, and snippets.

@lesimoes
Created May 2, 2020 19:53
Show Gist options
  • Save lesimoes/50bce8a1362f9e910e18dd8b689c3df4 to your computer and use it in GitHub Desktop.
Save lesimoes/50bce8a1362f9e910e18dd8b689c3df4 to your computer and use it in GitHub Desktop.
Sizeof and Malloc in C
#include <stdio.h>
int main(int argc, const char * argv[]) {
int tamanho;
printf("Digite um tamanho para o vetor: ");
scanf("%d", &tamanho);
//Aqui separamos um espaço em memório dinamicamente do tamanhao digitado pelo usuário
int *vetor = (int *) malloc(tamanho * sizeof(int));
for (int i = 0 ; i < tamanho ; i ++) {
vetor[i] = i + 1;
printf("%d ", vetor[i]);
}
printf("\n");
return 0;
}
#include <stdio.h>
int main(int argc, const char * argv[]) {
printf("Espaço de int: %d\n", sizeof(int));
printf("Espaço de double: %d\n", sizeof(double));
printf("Espaço de float: %d\n", sizeof(float));
printf("Espaço de char: %d\n", sizeof(char));
printf("\n");
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment