I am working on a Matrix Multiplication problem and I am dynamically allocating arrays
Here is what I currently have:
global:
int **a;
in my allocatematrix function: (m - row, k - col)
a = (int **)malloc(m * sizeof(int *));
for (i=0; i<m; i++)
{
a[i] = (int *)malloc(k * sizeof(int));
}
// Note that arr[i][j] is same as *(*(arr+i)+j)
// just to test my array allocation is working
for (i = 0; i < m; i++)
{
for (j = 0; j < k; j++)
{
a[i][j] = ++count; // OR *(*(arr+i)+j) = ++count
}
}
for (i = 0; i < m; i++)
{
for (j = 0; j < k; j++)
{
printf("%d ", a[i][j]);
}
printf("\n");
}
// I would like to pass my pointer to pointer
// into a subroutine, such that it transforms the
// the global double array, but it keeps blowing up here
loadMatrix(fp, &a, m, k);
load matrix function:
// read in the file
void loadMatrix(FILE *fp, int ***arr, int size1, int size2)
{
//fp = fopen(name, "r");
if (fp == NULL)
{
printf("Error while opening file!\n");
exit(0);
}
int i, j;
for(i = 0; i < size1; i++)
{
for(j = 0; j < size2; j++)
{
int value = 0;
fscanf(fp, "%d", &value);
printf("Current Value: %d\n", value);
//value = (((arr + i)) + j);
// line below is where I think I have the issue
*(*(*(arr+i)+j)) = value;
}
}
}
sample run: with this line commented ((*(arr+i)+j)) = value;
3 2 3
1 2
3 4
5 6
Current Value: 1
Current Value: 4
Current Value: 2
Current Value: 5
Current Value: 3
Current Value: 6
with out commented out:
3 2 3
1 2
3 4
5 6
Current Value: 1
Current Value: 4
Current Value: 2
Segmentation fault (core dumped)