Home > Net >  how can i return an array from a function
how can i return an array from a function

Time:10-01

How can I return an array from a function, I am trying to perform (3*3)*(3*1) matrix multiplication using this translation function and how can i get an array out of it.

#include <stdio.h>
#include <math.h>

int* translation(int x, int y, int tx, int ty) {
    static int res[3][1] = {0}, xy[3][1] = {{x},{y},{1}};
    int tm[3][3] = {{1,0, tx}, {0,1,ty}, {0,0,1}};
    for (int i = 0; i<3; i  ) {
        for (int j = 0; j<3; j  ) {
            res[i][0]  = tm[i][j]*xy[j][0];
        }
    }
    return res;
}

int main()
{
    int *arr[3][1];
    arr = translation(5, 5);
    printf("%d %d %d", arr[0][0], arr[0][1], arr[0][2]);
    return 0;
}

CodePudding user response:

"How can I return an array from a function"

You can't.

The language has no such concept.

You'll have to return something including the length to give the user of the function the information. In C the idiomatic approach is to supply a pointer to the function and to get a value (via that pointer) in return:

size_t no_idea;
void function(void *data, &no_idea);

As a user of this function you'd have to read no_idea before judging.

CodePudding user response:

you question is missing a lot of information like what you want to do with your code, the variable named xy isn't defined anywhere in your code, and so on...

but for clarification, if your result matrix is of unknown size, you can wrap your array into a struct, if you don't know what is the struct, you can refer to enter image description here

while if the size is known, so the explanation diagram may look like this:

enter image description here

and when you return, you can either return by value or by reference, but if you are going to return a struct by reference then you should declare it as static.

so you can do something like this (for clarification purposes, size of matrix is unknown):

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

typedef struct Array_t{
    size_t arrSize_x;
    size_t arrSize_y;
    int **arr;
}Array_t;

Array_t translation(int x, int y, int tx, int ty) {
    // create a struct holding the array
    Array_t res;
    res.arrSize_x = 3;
    res.arrSize_y = 1;

    // rows are stored in heap memory and initiated with zeros
    res.arr = (int**) calloc(res.arrSize_x, sizeof(int));

    // columns are also stored in heap memory and initiated with zeros
    for (int i = 0; i < res.arrSize_x;   i) {
        res.arr[i] = (int *) calloc(res.arrSize_y, sizeof(int));
    }

    res.arr[0][0] = 1;
    res.arr[1][0] = 2;
    res.arr[2][0] = 3;


    return res;
}

int main()
{
    Array_t array;
    // 1, 2, 3, 4 are dummy parameters
    array = translation(1, 2, 3, 4);
    printf("elements are :\n");
    for (int i = 0; i < array.arrSize_x;   i) {
        for (int j = 0; j < array.arrSize_y;   j) {
            printf("%d\t", array.arr[i][j]);
        }
        printf("\n");
    }

    return 0;
}

and this is the output:

elements are :
1
2
3

but if size of matrix is known then you can do something like this (for clarification purposes, size of matrix is known):

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

typedef struct Array_t{
    int arr[3][1];
}Array_t;

Array_t translation(int x, int y, int tx, int ty) {
    // create a struct holding the array
    Array_t res;
    
    res.arr[0][0] = 1;
    res.arr[1][0] = 2;
    res.arr[2][0] = 3;


    return res;
}

int main()
{
    Array_t array;
    // 1, 2, 3, 4 are dummy parameters
    array = translation(1, 2, 3, 4);
    printf("elements are :\n");
    for (int i = 0; i < 3;   i) {
        for (int j = 0; j < 1;   j) {
            printf("%d\t", array.arr[i][j]);
        }
        printf("\n");
    }

    return 0;
}

and this is the ouput:

elements are :
1
2
3
  •  Tags:  
  • c
  • Related