I am using dynamic initialization to generate two Matrices. But problem is that my debugger gives me error in the function "printArray". Can anybody help me why is this happening. I have just learnt dynamic initialization yesterday.
In main function, I take inputs from user about dimensions of matrices and you can observe that on the basis of these inputs I become able to dynamically specify the dimensions of my Matrix.
#include <stdio.h>
#include <stdlib.h>
int Function(int [], int , int);
void printArray(int **Matrix, int row, int column);
int row = 0, col = 0;
int **P = 0;
int main() {
printf("Give me Dimensions of Matrix 1 : : ");
printf("No .of Rows : ");
scanf_s("%d", &row);
printf("No .of Columns : ");
scanf_s("%d", &col);
Function(P, row, col);
printArray(P, row, col);
printf("Give me size of Matrix 2 : ");
printf("No .of Rows : ");
scanf_s("%d", &row);
printf("No .of Columns : ");
scanf_s("%d", &col);
Function(P, row, col);
printArray(P, row, col);
system("pause");
return 0;
}
int Function(int **A, int s, int c) {
A = malloc(row * sizeof(int *));
for (int i = 0; i < row; i++) A[i] = malloc(col * sizeof(int *));
for (int i = 0; i < s; i++) {
for (int j = 0; j < c; j++) {
A[i][j] = rand() % 10;
}
}
}
void printArray(int **Matrix, int row, int column) {
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
printf("%d ", Matrix[i][j]);
}
puts("");
}
puts("");
}