How do you pass double pointers in a function?

So, I declared a 2D array using double pointers.
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
38
39
#include <iostream>
using namespace std;
int main()
{
	int **ptr;
	int numofrow = 0;
	int numofcol = 0;
	
	cout << "Number of Rows : ";
	cin >> numofrow;
	cout << "Number of Coloumns : ";
	cin >> numofcol;
	
	
	ptr = new int* [numofrow];
	
	for(int i=0; i<numofrow; i++){
		ptr[i] = new int[numofcol];
	}
	
	for(int r=0; r<numofrow; r++)
	{
		for(int c=0; c<numofcol; c++)
		{
			cin >> ptr[r][c];
		}
	}
	
	cout << endl;
	
	for(int r=0; r<numofrow; r++)
	{
		for(int c=0; c<numofcol; c++)
		{
			cout << ptr[r][c] << " ";
		}
		cout << endl;
	}
}

This works fine.

If I do this;
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
38
39
40
41
42
43
#include <iostream>
using namespace std;
void data_IO(int **pointer,int numofrow, int numofcol)
{
	for(int r=0; r<numofrow; r++)
	{
		for(int c=0; c<numofcol; c++)
		{
			cin >> pointer[r][c];
		}
	}
	cout << endl;
	
	for(int r=0; r<numofrow; r++)
	{
		for(int c=0; c<numofcol; c++)
		{
			cout << pointer[r][c] << " ";
		}
		cout << endl;
	}
}
int main()
{
	int **ptr;
	int numofrow = 0;
	int numofcol = 0;
	
	cout << "Number of Rows : ";
	cin >> numofrow;
	cout << "Number of Coloumns : ";
	cin >> numofcol;
	
	
	ptr = new int* [numofrow];
	
	for(int i=0; i<numofrow; i++){
		ptr[i] = new int[numofcol];
	}
	data_IO(&ptr , numofrow , numofcol);
	
	return 0;
}

I get an error. How can I fix this?
You don't want to pass a pointer to ptr so get rid of & in front of it.
Thanks Peter87.

My teacher said that we should pass perimeters for pointers like this (&ptr). But the case was different here. Can you give some explanations?
If x is a double, then &x is a pointer to a double

So if ptr is a pointer to a pointer to an int, then &ptr is a pointer to a pointer to a pointer to an int.

If you're meant to pass a pointer to a pointer to an int, do not pass a pointer to a pointer to a pointer to an int.


Don't worry about this too much at this stage. It seems you're being taught advanced material that the majority of C++ programmers never bother with and really don't have much use for. This being C++, we have proper container objects so that we don't have to spend our time laboriously constructing pointers to pointers to pointers and keeping track of memory ourselves.
Topic archived. No new replies allowed.