I'd like to make a function that receives a 2-dimensional array and returns one of its rows ('which') as a simple array. I wrote this:
int *row(int *array, int lines, int columns, int which)
{
int result[columns];
for (int i=0; i<columns; i++)
{
result[i] = *array[which][i];
}
return result;
}
However, in line 7 I got the following error: invalid types 'int[int]' for array subscript. Any idea how to do this properly? I also tried to handle the 2D-array as an array of arrays, but didn't succeed. I'm novice, so please avoid too advanced concepts.
Thanks for the help!
UPDATE: thanks for the help! Now my code looks like:
int n; //rows
int m; //columns
int data[100][100];
int array[100];
int *row(int *array, int rows, int columns, int which)
{
int* result = new int[columns];
for (int i=0; i<columns; i++)
{
result[i] = *array[which*columns+i];
}
return result;
delete[] result;
}
int main()
{
array=row(data, n, m, 0);
}
I still get an error in main: incompatible types in assignment of 'int*' to 'int [100]'
What could be the problem now? I also don't know where to use the delete[] function to free up the array.
Thank you very much for the help!