How to return a multidimensional array with a function?

Hello

I would like to return a matrix with a function, can anyone help me?

grtz

Mike
You could just return it like any other array, since a matrix is just an array of arrays, just return the "outermost" array.
I hope this is what u want;)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdlib.h>
#include <stdio.h>

#define MAXRIX_SIZE 5

typedef  char  (*Matrix)[MAXRIX_SIZE][MAXRIX_SIZE]; 

Matrix getmartix()
{
	Matrix mat;
	printf("\n %d",sizeof(*mat));
	mat =(Matrix)calloc(1,sizeof *mat);
	printf("\n In Function");
	for (int i=0;i<MAXRIX_SIZE;i++)
	{
		for (int j=0;j<MAXRIX_SIZE;j++)
		{
			(*mat)[i][j] = i+j;
			printf("\n %d",(*mat)[i][j]);
		}
	};

	return mat;
}
int main()
{
	
   Matrix mm;
   mm = getmartix();

	printf("\n In Main");
	for (int i=0;i<MAXRIX_SIZE;i++)
	{
		for (int j=0;j<MAXRIX_SIZE;j++)
		{
			printf("\n %d",(*mm)[i][j]);                                                                    
		}
	};
	getchar();
	return 0;
}
Last edited on
OMG! That's horrible! Just because it compiles as C++ code doesn't mean you should use it. The reason the C++ compiler supports it is for backwards compatibility agains old C code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <stdlib.h>     // Don't use these in C++, use <cstdlib>
#include <stdio.h>      // <cstdio>

#define MAXRIX_SIZE 5   // Don't use macro, use const int

typedef  char  (*Matrix)[MAXRIX_SIZE][MAXRIX_SIZE]; // Pointer to an array? Ugly!

Matrix getmartix()
{
    Matrix mat;    // Uninitialized pointer
    printf("\n %d",sizeof(*mat));          // C
    mat =(Matrix)calloc(1,sizeof *mat);    // Don't use calloc, use new/delete
    printf("\n In Function");              // Why \n at start instead of end?
    for (int i=0;i<MAXRIX_SIZE;i++)
    {
        for (int j=0;j<MAXRIX_SIZE;j++)
        {
            (*mat)[i][j] = i+j;              // Cryptical syntax, hard to read
            printf("\n %d",(*mat)[i][j]);
        }
    };             // Don't put semicolon here

    return mat;    // Return pointer to allocated memory resource, who owns it?
}
int main()
{
    Matrix mm;       // Uninitialized pointer
    mm = getmartix();

    printf("\n In Main");
    for (int i=0;i<MAXRIX_SIZE;i++)
    {
        for (int j=0;j<MAXRIX_SIZE;j++)
        {
            printf("\n %d",(*mm)[i][j]);                                                                    
        }
    };             // Don't put semicolon here
    getchar();
    return 0; // MEMORY LEAK!
}


If you really need to return a two dimentional matrix, the way to do it in C++ it to make some kind of wrapper class, and either implement copy operations, or make it impossible to copy, and return a pointer wrapped in an auto_ptr, like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
using namespace std;

template<int w, int h>
class Matrix
{
public:
    Matrix() {}

    unsigned char& get(int x, int y) {
        return mat[y][x];
    }

private:
    // Disallow copy operations
    Matrix(const Matrix&);
    Matrix& operator=(const Matrix&);

    unsigned char mat[h][w];
};

const int MATRIX_SIZE = 5;
typedef Matrix<MATRIX_SIZE, MATRIX_SIZE> Matrix5x5;

std::auto_ptr<Matrix5x5> getMatrix()
{
    std::auto_ptr<Matrix5x5> mat(new Matrix5x5);
    for (int y=0; y<MATRIX_SIZE; y++)
    {
        for (int x=0; x<MATRIX_SIZE; x++)
        {
            mat->get(x,y) = x+y;
        }
    }
    return mat;
}

int main()
{
    std::auto_ptr<Matrix5x5> mat;

    mat = getMatrix();

    for (int y=0; y<MATRIX_SIZE; y++)
    {
        for (int x=0; x<MATRIX_SIZE; x++)
        {
            int v = mat->get(x,y);
            std::cout << v << std::endl;
        }
    }
    getchar();
}


The simpler alternative is to use a reference argument, and let main create the matrix.
Last edited on
closed account (z05DSL3A)
"OMG! That's horrible! ..."

Ouch! Bad day at the office? ;0)
Actually, it's been a great day. Been out skiing in the nice weather, and will go see "No country for old men" later :-)
Topic archived. No new replies allowed.