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?