I am working on a homework assignment to do matrix multiplication with dynamically allocated 2d arrays. I have written the following function to load the matrix:
void loadMatrix(FILE* fp, int** matrix, int rowSize, int colSize) {
for (int i = 0; i < rowSize; i++) {
for (int j = 0; j < colSize; j++) {
fscanf(fp, "%d", &matrix[i][j]);
}
}
}
I declare my matrix as a global variable as follows:
int **a;
and then initialize and load it as follows:
// allocate memory for the array rows
a = (int **) malloc(m * sizeof(int*));
// allocate memory for array columns
for (int i = 0; i < m; i++) {
a[i] = malloc(k * sizeof(int));
}
loadMatrix(fp, a, m, k);
Everything works as expected, however the function signature that the teacher provided is the following:
void loadMatrix(FILE*, int ***, int, int);
I tried using that signature, and pass in the address of the matrix using &, and removing the & from my loadMatrix function, thinking that the outputs should be the same, but using *** does not work as expected. What am I missing? Also what would be the advantage of using triple pointers if there is one?