fork download
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <malloc.h>
  6. void BuildMatrix(int*** matrix, int row, int column);
  7. void ExitAndFree(int*** matrix, int row);
  8. int main() {
  9. int row = 2, column = 2;
  10. int **matrix, i, j;
  11. BuildMatrix(&matrix, row, column);
  12.  
  13. printf("Your matrix:\n");
  14. for (i = 0; i < row; i++) { //just to check that it inserts all the values OK
  15. for (j = 0; j < column; j++) {
  16. printf("\n%d \n", matrix[i][j]);
  17. }
  18. }
  19. ExitAndFree(&matrix, row);
  20. return 0;
  21. }
  22. void BuildMatrix(int*** matrix, int row, int column) {
  23. int i, j;
  24. *matrix = (int**)malloc(row * sizeof(int *));
  25. for (i = 0; i < row; i++)
  26. (*matrix)[i] = (int*)malloc(column * sizeof(int));
  27. printf("\nPlease enter values to the matrix\n");
  28. for (i = 0; i < row; i++) {
  29. for (j = 0; j < column; j++) {
  30. scanf("%d", &((*matrix)[i][j]));
  31. }
  32. }
  33. }
  34. void ExitAndFree(int*** matrix, int row) {
  35. int i;
  36. for (i = 0; i < row; i++) {
  37. free((*matrix)[i]);
  38. (*matrix)[i] = NULL;
  39. }
  40. free(*matrix);
  41. matrix = NULL;
  42. }
Success #stdin #stdout 0s 2304KB
stdin
1 2 3 4
stdout
Please enter values to the matrix
Your matrix:

1 

2 

3 

4