0

I know how to pass 1D array pointer to a function by the following code

void fiddleWithArray(int*);   

int main(){

int list[10] = {1, 3, 5, 7, 9, 11, 13, 17};
cout << "List at 0 before being passed is... " << list[0][0] << endl;    
cout << "List at 1 before being passed is... " << list[1][0] << endl;    
fiddleWithArray(list);
cout << "List at 0 after being passed is... " << list[0][0] << endl;    
cout << "List at 1 after being passed is... " << list[1][0] << endl;    

}

void fiddleWithArray(int* input){
input[0] = 45;
input[1] = 18;
}

However, when I try to do something similar for a 2D array(as shown below) I get an error.

void fiddleWithArray (int** input);

int main ()
{
int list [10][2]={{1,3},{5,7},{9,11},{13,17},{7,4},{5,90},{9,1},{3,25}};
int ** pointer;
pointer=&list;
cout<< "List at 0 before being passed is ... "<< list[0][0]<< endl;
cout<< "List at 1 before being passed is ... "<< list[1][0]<< endl;
fiddleWithArray(pointer);
cout<< "List at 0 after being passed is ... "<< list[0][0]<< endl;
cout<< "List at 1 after being passed is ... "<< list[1][0]<< endl;
}

void fiddleWithArray(int** input)
{   
cout << input [6][1]<< endl;
}

The compiler gives an error saying "error: cannot convert ‘int (*)[10][2]’ to ‘int**’ in assignment pointer=&list;" I am also open to alternate methods of passing a 2D array pointer to a function.

Mc Missile
  • 585
  • 5
  • 20

2 Answers2

2

Keeping your data structure, if you want to pass list to fiddleWithArray you can declare it as

void fiddleWithArray (int input[][2]);

and then, in the main program, call it as

fiddleWithArray(list);

There is also another problem in your program: cout << list[0] does not work. If you want to print the contents of the array when the first index is fixed to 0 you would write something like

cout << list[0][0] << " " << list[0][1]

If you instead intended to write the array where the second index is fixed to 0 or 1, then, to keep things easily, you need a short loop like

for (unsigned int i = 0; i < 10; i++)
   cout << list[i][0] << " ";
cout << endl;

Finally, instead of using int[][] you may want to use std::arrayintroduced in C++11.

francesco
  • 4,122
  • 7
  • 15
  • 31
  • thanks a lot for the help. ur suggestion sorted out my issue. :) – Mc Missile Jun 29 '18 at 05:16
  • Good answer. Recommend changing `void fiddleWithArray (int input[][2]);` to `void fiddleWithArray (int input[][2], size_t len);` or something similar so `fiddleWithArray` knows when to stop iterating. – user4581301 Jun 29 '18 at 05:20
  • @ francesco also a small clarification. the [2] in void fiddleWithArray (int input[][2]); is it 2 because it is a 2d array or because the second parameter in list array is 2? – Mc Missile Jun 29 '18 at 06:13
  • 1
    @McMissile the [2] in the declaration of fiddleWithArray refers to the size of the second dimension. In fact, in declaring multidimensional arrays as parameters, you have to specify the sizes of all dimensions except possibly the first one. – francesco Jun 29 '18 at 07:30
0
void fiddleWithArray(int input[10][2])
{
    cout << input[6][1] << endl;
}

int main()
{
    int list[10][2] = {{1,3},{5,7},{9,11},{13,17},{7,4},{5,90},{9,1},{3,25}};
    int (*pointer)[10][2];
    pointer=&list;
    cout << "List at 0 before being passed is ... "<< list[0][0]<< endl;
    cout << "List at 1 before being passed is ... "<< list[1][0]<< endl;
    fiddleWithArray(*pointer);
    cout << "List at 0 after being passed is ... "<< list[0][0]<< endl;
    cout << "List at 1 after being passed is ... "<< list[1][0]<< endl;
}

Will be better using std::array