
Enviado em 02/01/2019 - 11:21h
Tenho o seguinte código:
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
int **m_malloc(size_t rows, size_t columns){
int **mat=malloc(sizeof(int*)*rows);
for(size_t i=0; i<rows; i++){
mat[i]=malloc(sizeof(int)*columns);
}
return mat;
}
void m_show(int *mat[], size_t rows, size_t columns){
printf("\n");
for(size_t i=0; i<rows; i++){
for(size_t j=0; j<columns; j++){
printf("[%d] ", mat[i][j]);
}
printf("\n");
}
}
void m_fill(int *mat[], size_t rows, size_t columns){
int value;
srand(time(NULL));
for(size_t i=0; i<rows; i++){
for(size_t j=0; j<columns; j++){
value=(int)rand()%100;
mat[i][j]=value;
}
}
}
void m_free(int *mat[], size_t columns){
for(size_t i=0; i<columns; i++){
free(mat[i]);
}
free(mat);
}
int main(void){
int **matrix;
size_t rows, columns;
printf("ROWS >");
scanf("%ld", &rows);
printf("COLUMNS >");
scanf("%ld", &columns);
matrix=m_malloc(rows, columns);
m_fill(matrix, rows, columns);
m_show(matrix, rows, columns);
m_free(matrix, columns);
return 0;
}
//matriz == matrix (in English) ???
localhost@MIG-21:~/Documents/C$ ./app
ROWS >5
COLUMNS >5
[5] [12] [13] [65] [26]
[91] [55] [29] [62] [50]
[46] [92] [60] [41] [29]
[2] [15] [5] [55] [13]
[60] [72] [0] [53] [0]
localhost@MIG-21:~/Documents/C$ ./app
ROWS >9
COLUMNS >3
[77] [35] [54]
[93] [83] [90]
[38] [59] [98]
[10] [58] [45]
[28] [84] [65]
[55] [65] [88]
[56] [50] [77]
[84] [5] [30]
[98] [55] [61]
localhost@MIG-21:~/Documents/C$ ./app
ROWS >1
COLUMNS >4
[30] [40] [16] [30]
Segmentation fault (core dumped)
IA Turbina o Desktop Linux enquanto distros renovam forças
Como extrair chaves TOTP 2FA a partir de QRCODE (Google Authenticator)
Linux em 2025: Segurança prática para o usuário
Desktop Linux em alta: novos apps, distros e privacidade marcam o sábado
Como usar Gpaste no ambiente Cinnamon
Atualizando o Fedora 42 para 43
Como saber se o seu e-mail já teve a senha vazada?
VOL já não é mais como antes? (9)
É normal não gostar de KDE? (13)
E aí? O Warsaw já está funcionando no Debian 13? [RESOLVIDO] (15)
Secure boot, artigo interessante, nada técnico. (4)
copiar library para diretorio /usr/share/..... su com Falha na a... (1)









