Trying to copy arrays.

Hi guys, was trying to copy over 1 array into the other with an element inserted.

The output for some reason is -858993460 -858993460 -858993460 -858993460 -858993460 -858993460 .. as in the arrOutput is empty..

Why? I know I am passing a pointer by value but that is just a pointer to the first element, I am copying the address of the original Array aren`t I?

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
 
void insertIntoArrayAnother(int * arr, int * arrOutput, int& n, int newElement)
{
	int i = 0;
	int k = 0;
	bool elSet = false;
	while (i < n)
	{
		if (arr[i] < newElement ||  elSet)
		{
			arrOutput[k] = arr[i];
			i++;
			k++;
		}
		else {
			arrOutput[k] = newElement;
			k++;
			elSet = true;
		}
	}

	n++;
}

int main()
{

	int arr[9]{ 1,2,3,4,6,7,8,9 }; // 8 elements
	int newArr[9];

	int n = 8; // this is how many elements can be in the array

	int newElement = 5;
	insertIntoArrayAnother(arr, &newArr[0], n, newElement);


	for (int i = 0; i < n; i++)
	{

		std::cout << newArr[n] << " ";
	}

	std::cout << std::endl;
	system("pause");
}
Never mind my stupid mistake of printing incorrect staff. Sorry guys.
Topic archived. No new replies allowed.