I try to understand the malloc and dynamic allocation with c, but when I compile the program everything is ok, but if I run it the terminal tells me Segmentation fault (core dumped) and exits
#include <stdio.h>
#include <stdlib.h>
int main(){
int **matrice;
int righe, colonne;
int r, c;
printf("Quante RIGHE deve avere la matrice? ");
scanf("%d", &righe);
printf("Quante COLONNE deve avere la matrice? ");
scanf("%d", &colonne);
matrice = (int**) malloc(righe*colonne*sizeof(int));
for(r=0; r<righe; r++){
matrice[r] = (int*) malloc(colonne*sizeof(int));
for(r=0; r<righe; r++){
for(c=0; c<colonne; c++){
printf("Elemento[%d][%d]: ",r, c);
scanf("%d", &matrice[r][c]);
}
// print out
for(r=0; r<righe; r++){
for(c=0; c<colonne; c++){
printf ("%d\n", matrice[r][c]);
}
}
}
}
}