Pointers as iterators - Help needed

A "Try this" in Chapter 20 has the following specs:
Write a function void copy(int* f1, int* e1, int* f2) that copies the elements
of an array of int s defined by [ f1 : e1 ) into another [ f2 : f2+(e1–f1) ). Use only
the iterator operations mentioned above (not subscripting).
The iterator operations mentioned above that are of course these ones:
p==q, p != q, *p, *p==val, val==*p, ++p
.

I'm having a hard time with this, though. Here's my 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
// Osman Zakir
// 11 / 8 / 2017
// Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition
// Chapter 20 Try This:
// Program to define a function void copy(int* f1, int* e1, int* f2) which copies the contents of one array into another.

#include "../../cust_std_lib_facilities.h"
#include <iostream>

void copy(int *f1, int *e1, int *f2);

int main()
{
	int *arr1 = new int[10];
	for (int i = 0; i < 10; ++i)
	{
		*(arr1 + i) = i + 1;
	}
	for (int i = 0; i < 10; ++i)
	{
		std::cout << *(arr1 + i) << '\n';
	}
	int *arr2 = new int[10];
	copy(arr1, arr1 + 10, arr2);
	for (int i = 0; i < 10; ++i)
	{
		std::cout << *(arr2 + i) << '\n';
	}
	delete[] arr1;
	delete[] arr2;
	keep_window_open();
}

void copy(int *f1, int *e1, int *f2)
{
	for (int i = *f1; i != (*f2 + (*e1 - *f1)); ++i)
	{
		*(f2 + i) = *(f1 + i);
		if (i >= 10)
		{
			break;
		}
	}
}


The output:
1
2
3
4
5
6
7
8
9
10
-842150451
2
3
4
5
6
7
8
9
10


The very fact that it seems like the array being copied into is bigger than the one being copied is already a problem for me. I don't understand how to make this work without making the index for the original array (the one being copied) go out of bounds. If I change the for loop to this (and if this is more how it should be):
1
2
3
4
for (int *i = f1; i != (f2 + (e1 - f1)); ++i)
{
		
}
, then how do I do the rest of it?

Thanks in advance.
Last edited on
The second loop is closer to how it should be done, but you need to use two iterators, one for each array. Then you can use the dereference operator (*) to access the array elements that the iterators refer to which is necessary when doing the copying of values from one array to the other.
Last edited on
Now I'm getting an error saying that some memory couldn't be read. This is how that function looks now, by the way:
1
2
3
4
5
6
7
void copy(int *f1, int *e1, int *f2)
{
	for (int *i = f1, *j = f2; i != (f2 + (e1 - f1)); ++i, ++j)
	{
		*j = *i;
	}
}
Maybe 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
#include <iostream>

void copy(int *f1, int *e1, int *f2);

int main()
{
  int org_numbers[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
  int new_numbers[10] = {0};

  copy(org_numbers, org_numbers + 10, new_numbers);

  for (int i : new_numbers)
    std::cout << i << "\n";
}

void copy(int *f1, int *e1, int *f2)
{
  while (f1 != e1)
  {
    *f2 = *f1;
    ++f1;
    ++f2;
  }
}
i is used to iterate the [ f1 : e1 ) range so to check if the end has been reached you should use i != e1. If you really want to check against (f2 + (e1 - f1)) you should instead use the j iterator, j != (f2 + (e1 - f1)).
Last edited on
Yeah, that seems to have worked.

1
2
3
4
5
6
7
void copy(int *f1, int *e1, int *f2)
{
	for (int *i = f1, *j = f2; j != (f2 + (e1 - f1)) && i != e1; ++i, ++j)
	{
		*j = *i;
	}
}


output:
1
2
3
4
5
6
7
8
9
10
1
2
3
4
5
6
7
8
9
10
Topic archived. No new replies allowed.