Strassen multiplication - c program
<p>We had to implement strassen's multiplication in a practical session , the code I wrote is given below , as you can see I have used a lot of intermediate matrices .I wanted to know how to return a 2d array from a function so that the code will look cleaner and more understandable , also it will give me some insights on pointers(a weak area for me )i.e say is use a double pointer as return type of sub function (int **sub(args list))
and since my strassen function has prototype strassen(int , int [],int[]..) 
When one argument of strassen function is a result of sub function , I get an error saying int (*)[] expected but returning int ** 
To resolve this I typecasted the result of sub function with int (*)[] but it does not work as expected 
Help please ? Thanks !</p>

<pre><code>#include&lt;stdio.h&gt;
#include&lt;stdlib.h&gt;

void add(int n,int a[n][n],int b[n][n],int result[][n])
{
printf("---add---\n");
int i,j;
//int **result = (int **)malloc(n*sizeof(int *));

/*for(i=0;i&lt;n;i++)
    result[i] = (int *)malloc(n*sizeof(int));*/

for(i=0;i&lt;n;i++)
{
    for(j=0;j&lt;n;j++)
    {
        result[i][j] = a[i][j] + b[i][j];
        printf("%d\t",result[i][j]);
    }
    printf("\n");
}
//return result;
}

   void sub(int n,int a[n][n],int b[n][n],int result[][n])
  {
printf("---sub---\n");
int i,j;
/*int **result = (int **)malloc(n*sizeof(int *));

for(i=0;i&lt;n;i++)
    result[i] = (int *)malloc(n*sizeof(int));*/

for(i=0;i&lt;n;i++)
{
    for(j=0;j&lt;n;j++)
    {
        result[i][j] = a[i][j] - b[i][j];
        printf("%d\t",result[i][j]);
    }
}

}

 void divide(int n,int a[n][n],int c[n/2][n/2],int i,int j)
 {
int i1,i2,j1,j2;
for(i1=0,i2=i;i1&lt;n/2;i1++,i2++)
{
    for(j1=0,j2=j;j1&lt;n/2;j1++,j2++)
    {
        c[i1][j1] = a[i2][j2];
    }
}
}

  void join(int n,int a[][n],int c[][n/2],int i,int j)      
 {
printf("join\n");
int i1,i2,j1,j2;
for(i1=0,i2=i;i1&lt;(n/2);i1++,i2++)
{
    for(j1=0,j2=j;j1&lt;(n/2);j1++,j2++)
    {
        a[i2][j2] = c[i1][j1];
        printf("c[%d][%d] %d\n",i1,j1,c[i1][j1]);
    }
}
}

 void multiply(int n,int a[][n],int b[][n],int result[][n])
{
int i,j;

if(n==2)
{
    //partial products
    printf("base case\n");
    int p1 = (a[0][0]+a[1][1])*(b[0][0]+b[1][1]);
    int p2 = (a[1][0]+a[1][1])*b[0][0];
    int p3 = a[0][0]*(b[0][1]-b[1][1]);
    int p4 = a[1][1]*(b[1][0]-b[0][0]);
    int p5 = (a[0][0]+a[0][1])*b[1][1];
    int p6 = (a[1][0]-a[0][0])*(b[0][0]+b[0][1]);
    int p7 = (a[0][1]-a[1][1])*(b[1][0]+b[1][1]);

    int c11 = p1 + p4 - p5 + p7;
    int c12 = p3 + p5;
    int c21 = p2 + p4;
    int c22 = p1 + p3 - p2 + p6;

    result[0][0] = c11;
    result[0][1] = c12;
    result[1][0] = c21;
    result[1][1] = c22;

    for(i=0;i&lt;2;i++)
    {
        for(j=0;j&lt;2;j++)
        {
            printf("%d\t",result[i][j]);
        }
        printf("\n");
    }

}

else
{
    int a11[n/2][n/2];
    int a12[n/2][n/2];
    int a21[n/2][n/2];
    int a22[n/2][n/2];

    int b11[n/2][n/2];
    int b12[n/2][n/2];
    int b21[n/2][n/2];
    int b22[n/2][n/2];

    //divide matrices A &amp; B into four parts
    divide(n,a,a11,0,0);
    divide(n,a,a12,0,n/2);
    divide(n,a,a21,n/2,0);
    divide(n,a,a22,n/2,n/2);

    divide(n,b,b11,0,0);
    divide(n,b,b12,0,n/2);
    divide(n,b,b21,n/2,0);
    divide(n,b,b22,n/2,n/2);

    //partial products

    int p1[n/2][n/2],p2[n/2][n/2],p3[n/2][n/2],p4[n/2][n/2],p5[n/2][n/2],p6[n/2][n/2],p7[n/2][n/2];

    int c11[n/2][n/2],c12[n/2][n/2],c21[n/2][n/2],c22[n/2][n/2];

    int i1[n/2][n/2],i2[n/2][n/2];

    add(n/2,a11,a22,i1);
    add(n/2,b11,b22,i2);
    multiply(n/2,i1,i2,p1);

    int i3[n/2][n/2];
    add(n/2,a21,a22,i3);
    multiply(n/2,i3,b11,p2);

    int i4[n/2][n/2];
    sub(n/2,b12,b22,i4);
    multiply(n/2,a11,i4,p3);

    int i5[n/2][n/2];
    sub(n/2,b21,b11,i5);
    multiply(n/2,a22,i5,p4);

    int i6[n/2][n/2];
    add(n/2,a11,a12,i6);
    multiply(n/2,i6,b22,p5);

    int i7[n/2][n/2];
    int i8[n/2][n/2];
    sub(n/2,a21,a11,i7);
    add(n/2,b11,b12,i8);
    multiply(n/2,i7,i8,p6);

    int i9[n/2][n/2];
    int i10[n/2][n/2];

    sub(n/2,a12,a22,i9);
    add(n/2,b21,b22,i10);
    multiply(n/2,i9,i10,p7);

    //for c11
    int r1[n/2][n/2];
    int r2[n/2][n/2];

    add(n/2,p1,p4,r1);        //sub operation
    sub(n/2,r1,p5,r2);        //sub operation
    add(n/2,r2,p7,c11);        //main operation

    //for c12
    add(n/2,p3,p5,c12);

    //for c21
    add(n/2,p2,p4,c21);

    //for c22
    int r3[n/2][n/2];
    int r4[n/2][n/2];

    add(n/2,p1,p3,r3);       //sub operation
    sub(n/2,r3,p2,r4);       //sub operation
    add(n/2,r4,p6,c22);       //main operation


    join(n,result,c11,0,0);
    join(n,result,c12,0,n/2);
    join(n,result,c21,n/2,0);
    join(n,result,c22,n/2,n/2);

    printf("---c11---\n");
    for(i=0;i&lt;n/2;i++)
    {
        for(j=0;j&lt;n/2;j++)
        {
            printf("%d\t",c11[i][j]);
        }
        printf("\n");
    }
    printf("---c12---\n");
    for(i=0;i&lt;n/2;i++)
    {
        for(j=0;j&lt;n/2;j++)
        {
            printf("%d\t",c12[i][j]);
        }
        printf("\n");
    }
    printf("---c21---\n");
    for(i=0;i&lt;n/2;i++)
    {
        for(j=0;j&lt;n/2;j++)
        {
            printf("%d\t",c21[i][j]);
        }
        printf("\n");
    }
     printf("---c22---\n");
    for(i=0;i&lt;n/2;i++)
    {
        for(j=0;j&lt;n/2;j++)
        {
            printf("%d\t",c22[i][j]);
        }
        printf("\n");
    }
    /*for(i=0;i&lt;n;i++)
    {
        for(j=0;j&lt;n;j++)
        {
            printf("%d\t",result[i][j]);
        }
        printf("\n");
    }*/

}
</code></pre>

<p>}</p>

<pre><code> int main()
 {
int n;
printf("Enter the order of the matrices(power of 2)\n");
scanf("%d",&amp;n);

int i,j;
int a[n][n],b[n][n];

printf("Enter first matrix\n");
for(i=0;i&lt;n;i++)
{
    for(j=0;j&lt;n;j++)
    {
        scanf("%d",&amp;a[i][j]);
    }
}
printf("Enter second matrix\n");
for(i=0;i&lt;n;i++)
{
    for(j=0;j&lt;n;j++)
    {
        scanf("%d",&amp;b[i][j]);
    }
}
printf("First matrix is \n");
for(i=0;i&lt;n;i++)
{
    for(j=0;j&lt;n;j++)
    {
        printf("%d\t",a[i][j]);
    }
    printf("\n");
}

printf("Second matrix is \n");
for(i=0;i&lt;n;i++)
{
    for(j=0;j&lt;n;j++)
    {
        printf("%d\t",b[i][j]);
    }
    printf("\n");
}

int r[n][n];
multiply(n,a,b,r);
printf("---RESULT OF MULTIPLICATION---\n");
for(i=0;i&lt;n;i++)
{
    for(j=0;j&lt;n;j++)
    {
        printf("%d\t",r[i][j]);
    }
    printf("\n");
}

return 0;
</code></pre>

<p>}</p>