in this code i am trying to convert a 2d array into 1d array. I have declared the 1d array inside the function newarr(). i am unable to return the address of array arr to the main function so that I can print it.this is getting resolved if I dynamically make the array inside the function newarr().why does it happen. why cannot i initialise the array with int arr[9] inside newarr().
#include<iostream>
using namespace std;
int *newarr(int a[][3]);
void printarr(int arr[]);
int main()
{ int i,j,z=0;
int* q;
int a[3][3];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cin>>a[i][j];
}
q=newarr(a);
cout<<"new array"<<endl;
printarr(q);
}
int *newarr(int a[3][3])
{ int i,j,z=0;
int arr[9];
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
arr[z]=a[i][j];
z++;
}
}
return arr;
}
void printarr(int *arr)
{ int i;
for(i=0;i<9;i++)
{
cout<<arr[i]<<" ";
}
}