# problem in returning a matrix(2d array) to main function

Discussion in 'C' started by rohanag, Oct 26, 2007.

1. ### rohanagNew Member

Joined:
Oct 21, 2007
Messages:
7
0
Trophy Points:
0
Occupation:
Studying
Location:
Jaipur
in my program, i've taken the input from user, passed the 2 input matrices to the addtn function which performs addition,
but the function is not returning the correct values to main,it is actually returning garbage values
Mainly, i want to learn how to return a 2d array to the main module

This is main module
Code:
```#include<stdio.h>
#include<conio.h>
void input(int (*in)[3]);
void main()
{
int mat1[3][3],mat2[3][3],choice;
int i,j,(*res)[3];
clrscr();

printf("\nEnter the elements of first matrix:\n");
input(mat1);
printf("\nEnter the elements of second matrix:\n");
input(mat2);

do
{
clrscr();
printf("\t\t\t 2) EXIT\n\n\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%d\t",*(*(res+i)+j));
}
}
getch();
break;
case 2:
exit(1);
default :
printf("Not in the list of choices. Enter from the list only!");
}

}while(1);
}
```
This is for input
Code:
```//********************************INPUT**************************
void input(int (*in)[3])
{
int i,j;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("\nEnter the Element of row,%d and col,%d: ",i+1,j+1);
scanf("%d",(*(in+i)+j));
}
}
getch();
clrscr();
}
```
For addition, the problem area, which is not returning correctly
Code:
```int *addtn(int (*x)[3],int (*y)[3])
{
int i,j,c[3][3];
for(i=0;i<3;i++)
for(j=0;j<3;j++)
c[i][j]=0;

for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
c[i][j]= *(*(x+i)+j)+ *(*(y+i)+j);    //performing addition here
}
}
getch();
clrscr();
return(c);             //returning here
}
```

Joined:
Oct 23, 2007
Messages:
16