Created
April 7, 2019 21:25
-
-
Save santiagosilas/3a4477bfac4bf191765375066c059a0c to your computer and use it in GitHub Desktop.
ANSI C - Alocação Dinâmica de Matriz
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 main(void) { | |
| // Alocação Dinâmica e Inicialização de uma Matriz | |
| // Aloca as linhas da matriz | |
| int **m = calloc(5, sizeof(int *)); | |
| // Aloca as colunas da matriz | |
| int i, j; | |
| for(i=0; i < 5; i++) | |
| m[i] = malloc(4 * sizeof(int)); | |
| // Inicializa a matriz | |
| srand(42); | |
| for(i=0; i< 5; i++) | |
| for(j=0; j < 4; j++) | |
| m[i][j] = 10 * (i+1) + rand() % 10; | |
| // Imprime a matriz | |
| for(i=0; i< 5; i++){ | |
| for(j=0; j < 4; j++) | |
| printf("%d\t", m[i][j]); | |
| printf("\n"); | |
| } | |
| // Desaloca a memória | |
| for(i=0; i < 5; i++) | |
| free(m[i]); | |
| free(m); | |
| return 0; | |
| } | |
| /** | |
| output: | |
| <int> length:4 byte(s) | |
| <long> length:8 byte(s) | |
| <char> length:1 byte(s) | |
| <float> length:4 byte(s) | |
| <double> length:8 byte(s) | |
| */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment