pointer arithmetic to add contents of two arrays

I'm trying to write a function that uses pointer arithmetic to ad the contents of two int arrays, putting the sums in a third array. Everything seems to fuction, except that it spits out zeros every time.

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
  #include <iostream>

using namespace std;

void AddArrays(int arrayOne[], int arrayTwo[], int arrayThree[], int arraySize);

int main()
{
	int arrayOne[50];
	int arrayTwo[50];
	int arrayThree[50];

	int arraySize;

	cout << "\nEnter the array size: ";
	cin >> arraySize;

	for (int i = 0; i < arraySize; i++)
	{
		cout << "Enter int " << i+1 << " for Array 1: ";
		cin >> arrayOne[i];
	}

	cout << endl;

	for (int j=0; j < arraySize; j++)
	{

		cout << "Enter int " << j+1 << " for Array 2: ";
		cin >> arrayTwo[j];
	}

	AddArrays(arrayOne, arrayTwo, arrayThree, arraySize);

	for (int k = 0; k < arraySize; k++)
	{
		cout << endl;
		cout << arrayThree[k] << endl;
	}

	return 0;
}

void AddArrays(int arrayOne[], int arrayTwo[], int arrayThree[], int arraySize)
{
	int *pointer1; //pointer to array one
	pointer1 = arrayOne;

	int *pointer2; //pointer to array two
	pointer2 = arrayTwo;

	int *pointer3; //pointer to array three
	pointer3 = arrayThree;

	for(int x; x < arraySize; x++)
	{
		*pointer3 = (*pointer1 + *pointer2);

		*pointer1++;
		*pointer2++;
		*pointer3++;
	}

}



Enter the array size: 3
Enter int 1 for Array 1: 1
Enter int 2 for Array 1: 2
Enter int 3 for Array 1: 3

Enter int 1 for Array 2: 1
Enter int 2 for Array 2: 2
Enter int 3 for Array 2: 3

0

0

0

Probably because you never initialize the index variable on line 55. :P
Topic archived. No new replies allowed.