I'm trying to do addition of matrices using pointers. But, in 3x3 matrix, only the first row of the matrix gets added, while the rest are zero. Please help me correct this.
Thank You!
My code is as follows ::

#include<stdio.h>
#include<conio.h>
int a[10][10],b[10][10],c[10][10]={0};
int i=0,j=0,k=0;
int r1,c1,r2,c2;
void *matrixAdd(int*,int*);
void printMatrix(int*);
int main()
{
     create();
     getch();
     return 0;
}
void create()
{
     printf("\nFor Matrix A --->\n");
     printf("\nEnter Number Of Rows :: ");
     scanf("%d",&r1);
     printf("\nEnter Number Of Columns :: ");
     scanf("%d",&c1);
     printf("\nFor Matrix B --->\n");
     printf("\nEnter Number Of Rows :: ");
     scanf("%d",&r2);
     printf("\nEnter Number Of Columns :: ");
     scanf("%d",&c2);
     printf("\n\nFOR MATRIX A :: \n");
     for(i=0;i<r1;i++)
     {
                      for(j=0;j<c1;j++)
                      {
                                       printf("Enter Element [%d][%d]",i,j);
                                       scanf("%d",&a[i][j]);
                      }
     }     
     printf("\n\nFOR MATRIX B :: \n");
     for(i=0;i<r2;i++)
     {
                      for(j=0;j<c2;j++)
                      {
                                       printf("Enter Element [%d][%d]",i,j);
                                       scanf("%d",&b[i][j]);
                      }
     }    
     print();
     matrixAdd(a,b);
}
void print()
{
     printf("\n\tMATRIX A :: \n");
     for(i=0;i<r1;i++)
     {
                      printf("\t");
                      for(j=0;j<c1;j++)
                      {
                                       printf("%d  ",a[i][j]);
                      }
                      printf("\n");
     }     
     printf("\n\tMATRIX B :: \n");
     for(i=0;i<r2;i++)
     {
                      printf("\t");
                      for(j=0;j<c2;j++)
                      {
                                       printf("%d  ",b[i][j]);
                      }
                      printf("\n");
     }     
}
void *matrixAdd(int *m1, int* m2)
{
    int *result;
    int col,row;

    if ((r1 != r2) || (c1 != c2)) 
    {
       printf("ERROR: Matrix size mismatch - %dx%d and %dx%d\n", r1,c1,r2,c2);
       return 0;
    }

    result=(int *)malloc(r1*c1*sizeof(int));

    if (result == 0) 
    {
       printf("ERROR: failed to allocate more memory !\n");
       return 0;
    }

    for (col=0; col < c1; col++) 
    {
        for (row=0; row < r1; row++) 
        {
            *(result+(col*r1)+row)=*(m1+(col*r1)+row)+*(m2+(col*r1)+row);
        }
    }
    printMatrix(result);
}
void printMatrix(int *result)
{
     int row, col;
     for (col=0; col < c1; col++) 
     {
         for (row=0; row < r1; row++) 
         {
             printf("%d ",*(result+(col*r1)+row));
         }
     printf("\n");
     }
}

You're trying to malloc() memory, but haven't included the required stdlib.h file.

Normally, you'd get an error message, but since you're also casting the results of malloc() in your program, that error is not sent.

Malloc returns a void pointer, so there are very few times that you need to cast it - it's handled automatically.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.