how to return multidimensional array

how to return multidimensional array that is in the function.

in the function ı want to take integers for the array and then ı wan to return them into the main function to use them.





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
#include <iostream>
#include <cmath>
#include <string>
using namespace std;
int* get_integers();

      int main()
      {
 
      int *array = get_integers();

          system("pause");
          
          return 0;
          }
          
int *get_integers()
{
int n=0;
cout<<" kaç tane numara girmek istersiniz"<<endl;

cin>>n;
int array1[n][n];
int *array=array1;

for ( int i = 0; i < n; i++ )
{
cout << "Enter an integer: ";
cin>>array1[0][i]; // for only first row and then ı change them in the main function

}


return array;
}  







You should create the array in your main, then pass it into the function as an argument:

1
2
3
4
5
6
7
8
9
10
11
int main()
{
    int array[5][5];
    get_integers(array);
    return 0;
}

void get_integers(array[][5])
{
//....
}


You have some other issues regarding the size of your array but that's a seperate issue, if you need help with it, just ask.
then will function not be pointer ?
The return value will be void. There will be an input/output argument which is a pointer.
ı want to take numbers from get_integers function for two dimensional arrays and then return them into the main function and then them into again another function..
Last edited on
1) You may not declare array setting its dimension with non-const expression. So this code

int array1[n][n];


is invalid.

2) Mulidimensional array is implicitly converted to a pointer to its first element in expressions. The first element of a multidimensional array is also an array with the dimension that is one less than the dimension of the original array. So this code

int *array=array1;


is also invalid.

3) You may not return a pointer to a local array because after exiting function local array is destroyed.

So if you want to keep your design you shall allocate dynamically memory for your multidimensional array. There are two methods of allocation a two-dimensional array. The first

int ( * array )[ n ] = new int[n][n];


The second

int **array = new int *[n];
for ( int i = 0; i < n; i++ ) array[i] = new int[n];
Last edited on
To summarize from Vlad, Two dimensional arrays are messy. An alternative which is nicer works like this:

An array of size N*M can be created like this:
int* array = new int[N*M];

Then to set/access a value in column x and row y, you would do this:
std::cout << array[x + N*y];

how will ı use these ?

after taking the some inputs get function as an multidimensional ı want to return them into the main function.
for example

int main()
{
*array=get_function;
x=array[1][2];

}


*get_function()
{
....
...
....


return array;
}







ı can read any tutorial ı could not find.



Last edited on
You can do it for example the following way

int main()
{
int **array=get_function() ;
x=array[1][2];
...
for ( int i = 0; i < n; i++ ) delete [] array[i];
delete []array;
}


int **get_function()
{
....
...
....
int **array = new int *[n];
for ( int i = 0; i < n; i++ ) array[i] = new int[n];


return array;
}

But I think you should change your design. You should ask for dimension in the main and use it as a parameter of get_function. Otherwise you will not know what is dimension of your array.

For example

int **get_function( int n );

where n is desired dimension.
Last edited on
Topic archived. No new replies allowed.