Problem with array

Hi everybody! I have a problem with an array. I really need somebody's help. Here is a code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

#define COL 3
#define ROW 5

using namespace 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**)'
Change the function declaration from
int** func(int** mas)

to
int** func(int mas[][ROW])
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:
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
#include <iostream>

#define COL 3
#define ROW 5

using namespace 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?
closed account (28poGNh0)
enjoy

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
# include <iostream>

# define COL 3
# define ROW 5

using namespace std;

int** func(int mas[][ROW])
{
    int **tmpVar;
    tmpVar = new int*[ROW];
    for(int i=0;i<ROW;i++)
        tmpVar[i] = new int[COL];

    for(int i=0;i<ROW;i++)
    for(int j=0;j<COL;j++)
        tmpVar[j][i] = mas[j][i];

    return tmpVar;
}

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); //The error is not here !

    for(int i=0;i<ROW;i++)
    {
        for(int j=0;j<COL;j++)
        cout << p[j][i] << " ";
        cout << endl;
    }

    return 0;
}
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.
Last edited on
Techno01, as I understood one thing I had to do was inserting array into the dynamical memory?
Techno01, thank you, man. Your advise really helped.
closed account (28poGNh0)
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

all the best
Topic archived. No new replies allowed.