My question is why I can't associate directly the mul matrix address to a2 matrix pointer. This is a recursive function for matrix power, where a is the matrix we want to raise, and a2 is the result matrix, p is the power and n is the size. I'm a student and I'm new to pointers, and this was the only way I could find to make that pow function for matrix. I have this code.
void **pow(int **a, int p, int n, int **a2)
{
int **mul, i, j, k;
mul = (int**)malloc(2 * sizeof(int*));
for (i = 0; i < 2; i++)
*(mul + i) = (int*)malloc(2 * sizeof(int));
if (p == 0)
{
a2 = (int**)malloc(2 * sizeof(int*));
for (i = 0; i < 2; i++)
*(a2 + i) = (int*)malloc(2 * sizeof(int));
a2[0][0] = 1;
a2[0][1] = 0;
a2[1][0] = 0;
a2[1][1] = 1;
return 0;
}
else
{
pow(a, p - 1, n, a2);
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
{
mul[i][j] = 0;
for (k = 0; k < n; k++)
mul[i][j] = mul[i][j] + a[i][k] * a2[k][j];
printf("%d ", mul[i][j]);
}
printf("\n");
for (i = 0; i < n; i++)
for (j = 0; j < n; j++)
a2[i][j] = mul[i][j];
//WHY a2 = mul doesnt work
}
}
int main()
{
int **a;
int **b;
int i, j;
a = (int**)malloc(2 * sizeof(int*));
for (i = 0; i < 2; i++)
*(a + i) = (int*)malloc(2 * sizeof(int));
b = (int**)malloc(2 * sizeof(int*));
for (i = 0; i < 2; i++)
*(b + i) = (int*)malloc(2 * sizeof(int));
a[0][0] = 1;
a[0][1] = 2;
a[1][0] = 2;
a[1][1] = 3;
b[0][0] = 1;
b[0][1] = 0;
b[1][0] = 0;
b[1][1] = 1;
pow(a, 3, 2, b);
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
printf("%d ", *(*(b + i) + j));
printf("\n");
}
return 0;
}