#include <iostream>
#define COL 3
#define ROW 5
usingnamespace std;
int** func(int** mas)
{
//some code
return mas;
}
int main()
{
int arr[COL][ROW]={{2,3,1,4,5},{12,32,43,1,23},{6,65,7,43,11}};
int **p;
p=func(arr); //In that line error, but I don't know how to fix it
return 0;
}
The compiler gives me an error: cannot convert 'int (*)[5]' to 'int**' for argument '1' to 'int** func(int**)'
Thanks man it helped. But compiler has given me another error: cannot convert 'int(*)[5]' to 'int**' in return. It has given me that mistake in line return mas;.
I'm a bit confused because with 1 dimensional arrays such kind of function works, but with 2d doesn't and I don't know what to do.
Well, maybe you don't need to return the array, as the calling function already has the array. Any changes made to the values in the array will affect the original array, since it is passed as a pointer (even if it doesn't look like it).
However, if you for some reason really do want to make that the return value, just put return (int **) mas;
I corrected my code and have another problem. After compiling my compiler gave me 2 errors:
first: cannot convert 'int (*)[5]' to 'int**' in return
second: cannot convert 'int**' to 'int(*)[5]' in assignment
My purpose in this program to return pointer to changed array
Here is a code I have fixed, but it still doesn't work:
#include <iostream>
#define COL 3
#define ROW 5
usingnamespace std;
int** func(int mas[COL][ROW])
{
int (*p)[ROW];
p=mas;
//some code
return p; //mistake in this line
}
int main()
{
int arr[COL][ROW]={{2,3,1,4,5},{12,32,43,1,23},{6,65,7,43,11}};
int (*p)[ROW];
p=arr;
p=func(arr); //and in that one
return 0;
}
My purpose in this program to return pointer to changed array
Can you please clarify what this means? Is it the contents of the array which are to be changed? Or do you want to return a new, completely different array?
in this function func I want to sort array and return pointer to sorted array (as I understand after passing array to the function and soting it, compiler create new array and I want just pointer to it). I have just begun studying 2d arrays.
Sorry, if I did something silly.
I am sorry for being late was solving another problem at less I hope
well your problem need a function to return a double pointer if you want to change in the function that possible too just ask the right question and hope to get the right answer