In the main function, I have one two-dimensional array, A. I need to pass A into a function f, which takes A, may change A. f will also generate another two-dimensional array B based on some operations on A and return A.

I know it is about passing pointer passing but do not get the solution after reading the book.

How to define this function and how to call it? Thanks.

Hi, first post some code to let us know about your problem, second post your array to let us know how they looks like, it have 4,5, 1000 rows? lines?

A tip: If you wanna to separate the array, to make calcs with the number, you can do a loop, like:

for(unsigned m =0; m < 10; m++)
{
  arrayB[m][m] = arrayA[m][m];     
}

This just copy the number of arrayA in arrayB.

commented: The Example doesn't copy the Array from A to B -1

Post in your code on what you're attempts were in solving the problem.

There are lots and lots of IF's and BUT's when we take this question into consideration. Firstly I would like to ask you if the arrays being passed are of Constant Size, if so, the solution might become very Straight Forward.

But, If your array size for the function isn't known, it raises an almost different method/mechanism to send it.

Please be a bit more clear about what you exactly want to do. And also Post down your code.

Hello winecoding,
You can pass 2d arrays as pointers.

int func(int **array1);

The function can now get a 2d array.
or
You can also pass arrays to a function like this.

int func(int a[][100]);

The size of the column should be given. You can call the function in the main like this.

int array1[100][100];
func(array1);

Hi Friends:

Here is a test code I write for this question:

#include <iostream>

int **func(int **array1, int **array2, int n){
a1 = new int[n][n];
for (int i = 0; i<n; i++){
for (int j = 0; j<n; j++)
a1[i][j]=array1[i][j]+array2[i][j];
}
return a1;
}

int main( ){

int m1[2][2];
int m2[2][2];

m1[0][0]=1; m1[0][1]=2; m1[1][0]=3; m1[1][1]=4;
m2[0][0]=1; m2[0][1]=2; m2[1][0]=3; m2[1][1]=4;

int **m3;
m3 = func(m1,m2,2);

return 0;
}

I think the function I implement is not correct, since it cannot pass the compiler with the following error message. Thanks for the help.

test2.cc: In function ‘int** func(int**, int**, int)’:
test2.cc:6: error: ‘a1’ was not declared in this scope
test2.cc:6: error: ‘n’ cannot appear in a constant-expression
test2.cc: In function ‘int main()’:
test2.cc:23: error: cannot convert ‘int (*)[2]’ to ‘int**’ for argument ‘1’ to ‘int** func(int**, int**, int)’

You cannot give a variable in a1 = new int[n][n];. Constant expression should be given

a1= new int[4][4]

.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.