Home > Back-end >  How do I return a 2D array in C?
How do I return a 2D array in C?

Time:12-13

I have an array constructed in a function. I want to return the array and store the value in another array. Somehow it does not work. It does not show any kind of errors but it just doesn't work.


int** matrixMultiplier(int A[2][2], int B[2][2], int dimension){
    
    int p, q, r, s, t, u, v, i, j;
    int C[2][2];
    
    p = (A[0][0]   A[1][1]) * (B[0][0]   B[1][1]);
    q = (A[1][0]   A[1][1]) * B[0][0];
    r = A[0][0]*(B[0][1] - B[1][1]);
    s = A[1][1]*(B[1][0] - B[0][0]);
    t = (A[0][0]   A[0][1]) * B[1][1];
    u = (A[1][0] - A[0][0]) * (B[0][0]   B[0][1]);
    v = (A[0][1] - A[1][1]) * (B[1][0] B[1][1]);

    C[0][0] = p s-t v;
    C[0][1] = r t;
    C[1][0] = q s;
    C[1][1] = p-q r u;

    return C;

}

void main(){

    int A[2][2] = {{2, 3},
                   {4, 5}};
    int B[2][2] = {{4, 3},
                   {7, 8}};
    
    int p, q, r, s, t, u, v, i, j;
    int **C = matrixMultiplier(A, B, 2);
    

    for(i = 0; i<2; i  ){
        for(j=0; j<2; j  ){
            printf("%d\t", C[i][j]);
        }
        printf("\n");
    }

}```

CodePudding user response:

C is declared as a local variable, meaning that it gets destroyed when the function ends. The two easiest ways to prevent it from getting destroyed are:

  1. Use malloc in the function matrixMultiplier like:
int i;
int ** C = malloc(sizeof(int*) * 2);
for (i = 0; i < 2; i  ) C[i] = malloc(sizeof(int) * 2);

or 2. Pass the array C as a function argument:

void matrixMultiplier(int A[2][2], int B[2][2], int dimension, int **C) {... 

and in main():

int C[2][2];
matrixMultiplier(A, B, 2, C);

CodePudding user response:

Your program gives multiple warnings when you compile it:

prog.c: In function ‘matrixMultiplier’:
prog.c:19:12: warning: return from incompatible pointer type [-Wincompatible-pointer-types]
     return C;
            ^
prog.c:19:12: warning: function returns address of local variable [-Wreturn-local-addr]

these are the two main ones, and show what you have wrong.

The first one shows a basic confusion as to what a "2D array" is. This is a common problem in C, as it does not have a fixed concept of a "2D array". Instead it has arrays and pointers, which are two different things, and are composable. In particular you can have an array of arrays (what you've declared C as) which can act like a 2D array, and you can also have the completely different array of pointers to arrays which can also act like a 2D array and appears to be what you are trying to return. But since "array of arrays" and "array of pointers" are two different incompatable things, you can't do this. You can fix this by changing the return type to int (*)[2], which is most easily done with a typedef:

typedef int (*matrix)[2];
matrix matrixMultiplier(int A[2][2], int B[2][2], int dimension) {

or can be done directly as

int (*matrixMultiplier(int A[2][2], int B[2][2], int dimension))[2] {

this latter syntax is pretty opaque, which is why using a typedef to make it clearer is suggested.

Then you have the second problem -- returning a pointer to a local variable (which goes out of scope, so leaves the pointer dangling). To fix that, you need to return a pointer to something that will live on after the function returns. You could use malloc to do that (instead of using a local var):

int (*C)[2] = malloc(2*2*sizeof(int));

or you could make C static

static int C[2][2];

in which case there will be a single C object created and reused for every call to the function.

  • Related