I have a simple function called make5 that makes every element in a 2d matrix equal to 5, shown below:
int make5(int r, int c, double **d)
{
int i, j;
for (i = 0; i < r; i++) {
for (j = 0; j < c; j++) {
d[i][j] = 5;
}
}
return 0;
}
I’d like to be able to run this function on different matrices, using the following calls:
make5(2, 3, a);
make5(2, 4, b);
where a and b have been declared as pointers to arrays of arrays. But when I try this I keep getting a segmentation fault error. How can I change make5 so I can run it on both a and b?
The function works just fine. You must be allocating the memory incorrectly or passing wrong parameters to the function.
Working example:
Output (ideone):