#include <iostream>
#include <cmath>
#include <string>
usingnamespace 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;
}
ı 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..
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];
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.