Pointer to pointer

I have a code

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
  #include <iostream>
using namespace std;
int arr[20];
int foo[20];
int bar[20];
int baz[20];
void initialize()
{
	int i;
	for(i = 0; i < 20; i++) 
	{
		arr[i] = i;
	}
	for(i = 0; i < 20; i++) 
	{
		foo[i] = i * 2;
	}
	for(i = 0; i < 20; i++)
	{
		bar[i] = i * 5;
	}
	for(i = 0; i < 20; i++)
	{
		baz[i] = i * 10;
	}
}
void swapPointer(int **pp1, int **pp2)
{
	int *ptemp = **pp1;
	**pp1 = **pp2;
	**pp2 = *ptemp;
}
void printArray(int *pArr)
{
	int i;
	for(i = 0; i < 20; i++)
		cout << pArr[i] << ' ';
	cout << endl;
}
int main()
{
	initialize();

	int i;
	int x, y;
	int *pArr[4];
	pArr[0] = arr;
	pArr[1] = foo;
	pArr[2] = bar;
	pArr[3] = baz;

	cout << "Before : " << endl;

	for(i = 0; i < 4; i++) 
	{
		cout << "pArr[" << i << "] : ";
		printArray(pArr[i]);
	}

	cout << endl;

	x = 0, y = 3;
	cout << "Swapping pArr[" << x << "] and pArr[" << y << "]..." << endl << endl;
	
	swapPointer(&pArr[x], &pArr[y]);

	cout << "After : " << endl;

	for(i = 0; i < 4; i++) 
	{
		cout << "pArr[" << i << "] : ";
		printArray(pArr[i]);
	}

	cout << endl;
}

I expect the output will be
[code]
Before :
pArr[0] : 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
pArr[1] : 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38
pArr[2] : 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95
pArr[3] : 0 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190

Swapping pArr[0] and pArr[3]...

After :
pArr[0] : 0 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190
pArr[1] : 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38
pArr[2] : 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95
pArr[3] : 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

Swapping pArr[0] and pArr[3]...
Swapping pArr[0] and pArr[2]...

After :
pArr[0] : 0 5 10 15 20 25 30 35 40 45 50 55 60 65 70 75 80 85 90 95
pArr[1] : 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38
pArr[2] : 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
pArr[3] : 0 10 20 30 40 50 60 70 80 90 100 110 120 130 140 150 160 170 180 190
[code]
But it not compile! I have tried everthing, sometimes it works but it didn't have the output like this! What did I do wrong?
You need to distinguish and know the difference between int** and int*

int* points to address of int
int** points to address of int*

With that said
Line 46: You need to declare int** in order to store 2D Array
Line 29: It does not make sense that you are assigning int** variable to int* variable.

Line 65: That's not how you pass pointer. Also, you are passing int* but the function swapPointer() accepts int**

Line 27: You are accepting COPIES of the pointer variables. So even if you swap the pointers, the pointer variables in main() will remain unchanged

Fix the swapPointer like this
1
2
3
4
5
6
7
// Accepts two int* reference parameters
void swapPointer(int* &pp1, int* &pp2)
{
	int* ptemp = pp1;
	pp1 = pp2;
	pp2 = ptemp;
}


Fix main() like 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
44
45
int main()
{
	initialize();

	int i;
	int x, y;

	int** pArr = new int*[4];

        // Allocate memory
	for( int i = 0 ; i < 4 ; i++ ) {
            pArr[i] = new int[20];
	}

	pArr[0] = arr;
	pArr[1] = foo;
	pArr[2] = bar;
	pArr[3] = baz;


	cout << "Before : " << endl;

	for(i = 0; i < 4; i++)
	{
		cout << "pArr[" << i << "] : ";
		printArray(pArr[i]);
	}

	cout << endl;

	x = 0, y = 3;
	cout << "Swapping pArr[" << x << "] and pArr[" << y << "]..." << endl << endl;

	swapPointer( pArr[x], pArr[y] );

	cout << "After : " << endl;

	for(i = 0; i < 4; i++)
	{
		cout << "pArr[" << i << "] : ";
		printArray(pArr[i]);
	}

	cout << endl;
}
Last edited on
How about like 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <iostream>
using namespace std;
int arr[20];
int foo[20];
int bar[20];
int baz[20];
void initialize()
{
	int i;
	for(i = 0; i < 20; i++) 
	{
		arr[i] = i;
	}
	for(i = 0; i < 20; i++) 
	{
		foo[i] = i * 2;
	}
	for(i = 0; i < 20; i++)
	{
		bar[i] = i * 5;
	}
	for(i = 0; i < 20; i++)
	{
		baz[i] = i * 10;
	}
}
void swapPointer(int **pp1, int **pp2)
{
	int *ptemp = *pp1;
	*pp1 = *pp2;
	*pp2 = ptemp;
}
void printArray(int *pArr)
{
	int i;
	for(i = 0; i < 20; i++)
		cout << pArr[i] << ' ';
	cout << endl;
}
int main()
{
	initialize();

	int i;
	int x, y;
	int *pArr[4];
	pArr[0] = arr;
	pArr[1] = foo;
	pArr[2] = bar;
	pArr[3] = baz;

	cout << "Before : " << endl;

	for(i = 0; i < 4; i++) 
	{
		cout << "pArr[" << i << "] : ";
		printArray(pArr[i]);
	}

	cout << endl;

	x = 0, y = 3;
	cout << "Swapping pArr[" << x << "] and pArr[" << y << "]..." << endl << endl;
	
	swapPointer(&pArr[x], &pArr[y]);

	cout << "After : " << endl;

	for(i = 0; i < 4; i++) 
	{
		cout << "pArr[" << i << "] : ";
		printArray(pArr[i]);
	}

	cout << endl;
}
Last edited on
> How about using pointer to pointer like **pp1, **pp2?

But you are only swapping int* and int*.

Note that pArr[x] and pArr[y] are just pointers (int*), not pointer to pointer (int**).

If you want to swap pointer to pointer, you can create another int** pArr ( With a different identifier, of course ), and change the swapPointer function accordingly and pass pointer to pointer variable

 
swapPointerToPointer( pArr , pArr2 );


EDIT:
Your code actually should work. I see what you intended to do now.
You want to pass the address of the pointers and swap them using pointer to pointer.
Sorry I missed your point earlier.

Also, because you initially created int arrays at the beginning of the program, you don't actually have to create int** pArr and allocate memory either. I actually learned from this. :)
Last edited on
Yeh I acctually learn from you too! Thank you!
Topic archived. No new replies allowed.