0

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;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • [dont cast malloc](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Barmar May 28 '20 at 14:36
  • You have lots of memory leaks. You never free any of the `a2` or `mul` arrays. – Barmar May 28 '20 at 14:40
  • Why does `mul` need to be dynamically allocated? Just use `int mul[2][2];` – Barmar May 28 '20 at 14:41
  • @Barmar yeah, I know, I'll change this. – Foreastbtch May 28 '20 at 14:41
  • And the same thing for `a2`: `int a2[2][2];` – Barmar May 28 '20 at 14:41
  • All your arrays are fixed size, why do you need any dynamic allocation at all? – Barmar May 28 '20 at 14:42
  • And they're not returned outside the function that creates them. – Barmar May 28 '20 at 14:43
  • I tried to work with pointers, wanna learn more about them. But I am asking why I cant give to a2 the address of mul at the end of the pow function. – Foreastbtch May 28 '20 at 14:44
  • The code is working, but I tried to replace for (i = 0; i < n; i++) for (j = 0; j < n; j++) a2[i][j] = mul[i][j]; with a2 = mul and is not working. – Foreastbtch May 28 '20 at 14:45
  • 1
    If you want to update the caller's variable, you have to pass its address: `&b`. And change the parameter to `int ***a2`. See the linked question for all the details. – Barmar May 28 '20 at 14:46
  • 1
    You can use poiinter to array notation `int (*mul)[2] = malloc(2 * 2 * sizeof(int));` is the same as `mul[2][2]` notation wise but with dynamic memory, you can then declare a pointer `int (*ptr)[2];` and assign `ptr = mul;`. – anastaciu May 28 '20 at 14:56

0 Answers0