Created
May 2, 2020 19:53
-
-
Save lesimoes/50bce8a1362f9e910e18dd8b689c3df4 to your computer and use it in GitHub Desktop.
Sizeof and Malloc in C
This file contains 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> | |
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; | |
} |
This file contains 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> | |
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