Saturday, August 27, 2011

Returning Two Dimensional Array from a Function in C

Many authors do not include topics about returning an array from a function in their books or articles. It is because, in most of the cases, there is no need of array to be returned from a function. Since, on passing the array by its name, the address of its first member is passed and any changes made on its formal arguments reflects on actual arguments.

But sometimes, there may arise the situation, where an array has to be returned from a function, for example, multiplying two matrices and assigning the result to another matrix. This can be done by creating a two-dimensional array inside a function, allocating a memory and returning that array. The important thing is you need to FREE the matrix that was allocated.

The source code of returning a two-dimensional array with reference to matrix addition is given here.
#include <stdio.h>
#include <stdlib.h>

int **matrix_sum(int matrix1[][3], int matrix2[][3]){
    int i, j;
    int **matrix3;
    matrix3 = malloc(sizeof(int*) * 3);
    
    for(i = 0; i < 3; i++) {
        matrix3[i] = malloc(sizeof(int*) * 3);
    }

    for(i = 0; i < 3; i++){
        for(j = 0; j < 3; j++){
            matrix3[i][j] = matrix1[i][j] + matrix2[i][j];
        }
    }
    return matrix3;
}



int main(){
    int x[3][3], y[3][3];
    int **a;
    int i,j;


    printf("Enter the matrix1: \n");
    for(i = 0; i < 3; i++){
        for(j = 0; j < 3; j++){
            scanf("%d",&x[i][j]);
        }
    }


    printf("Enter the matrix2: \n");
    for(i = 0; i < 3; i++){
        for(j = 0; j < 3; j++){
            scanf("%d",&y[i][j]);
        }
    }


    a = matrix_sum(x,y); //asigning
    printf("The sum of the matrix is: \n");
    for(i = 0; i < 3; i++){
        for(j = 0; j < 3; j++){
            printf("%d",a[i][j]);
            printf("\t");
        }
        printf("\n");
    }

    //free the memory
    for(i = 0; i < 3; i++) {
        free(a[i]);
    }
    free(a);
    return 0;
}

Or you can use a structure that has a two-dimensional array as a member. Create an instance of structure inside the function and return it.  The code for this approach is given below.
#include <stdio.h>
#include <stdlib.h>

typedef struct {
 int m[3][3];
} arr2d;

arr2d matrix_sum(int matrix1[][3], int matrix2[][3]){
    int i, j;
    arr2d result;

    for(i = 0; i < 3; i++){
        for(j = 0; j < 3; j++){
            result.m[i][j] = matrix1[i][j] + matrix2[i][j];
        }
    }
    return result;
}

int main(){
    int x[3][3], y[3][3];
    arr2d result;
    int i,j;

    printf("Enter the matrix1: \n");
    for(i = 0; i < 3; i++){
        for(j = 0; j < 3; j++){
            scanf("%d",&x[i][j]);
        }
    }

    printf("Enter the matrix2: \n");
    for(i = 0; i < 3; i++){
        for(j = 0; j < 3; j++){
            scanf("%d",&y[i][j]);
        }
    }

    result = matrix_sum(x,y);
    printf("The sum of the matrix is: \n");
    for(i = 0; i < 3; i++){
        for(j = 0; j < 3; j++){
            printf("%d",result.m[i][j]);
            printf("\t");
        }
        printf("\n");
    }

    return 0;
}

8 comments:

  1. Awesome Code, Helped a lot, take away a lot of headache. Thanks

    ReplyDelete
  2. thank u...it helped me a lot...

    ReplyDelete
  3. Sorry but you are just modifying the contents of matrix1 in the function using its base address and returning it back to main.
    What is something expected is to create an array inside the called function and return it to the calling function(i.e main in this case).
    is there any simple way out for doing so?

    ReplyDelete
    Replies
    1. The code is corrected to make it more reasonable. Thank you!!

      Delete