Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   problem in returning a matrix(2d array) to main function (http://www.go4expert.com/forums/returning-matrix-2d-array-main-function-t7060/)

 rohanag 26Oct2007 19:41

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

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: C

`#include<stdio.h>#include<conio.h>void input(int (*in)[3]);int *addtn(int (*x)[3],int (*y)[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 1) ADDITION\n");    printf("\t\t\t 2) EXIT\n\n\n");    printf("\n\nEnter Your choice: ");    scanf("%d",&choice);        switch(choice)        {        case 1:            res=addtn(mat1,mat2);            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: C

`//********************************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: C

`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}`

 dharmaraj.guru 29Oct2007 11:40

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

Can you please give me the warning/error message if you get?
If no such, have you tried to print that array?

 All times are GMT +5.5. The time now is 03:47.