What is going on in this program?

Can someone please give a trace/breakdown of whats happening in the program below?

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
#include <iostream>
using namespace std;

void increment_all (int* start, int* stop)
{
  int * current = start;
  while (current != stop) {
    ++(*current);  // increment value pointed
    ++current;     // increment pointer
  }
}

void print_all (const int* start, const int* stop)
{
  const int * current = start;
  while (current != stop) {
    cout << *current << '\n';
    ++current;     // increment pointer
  }
}

int main ()
{
  int numbers[] = {10,20,30};
  increment_all (numbers,numbers+3);
  print_all (numbers,numbers+3);
  return 0;
}


Im particularly confused about the (numbers+3) argument. That would make it the numbers[3] element in the array which doesn't even exist. So how is it being passed as an argument?



Yea, dont you wish people would comment their code. :P
I dont think commenting is the problem here. I got this code directly form the tutorials section of this website: http://www.cplusplus.com/doc/tutorial/pointers/ (see section "Pointers and Const")

However, its not explained clearly enough.
Last edited on
Comments is part of the problem, for one thing there is no description of what the code does. Good comments would keep you from asking what your asking...
Actually, I know what the code does. It simply increments the elements in the array and prints them. What I dont understand is HOW it works.

But I do agree. Comments are important for sure. You can't always figure out what a program does just by looking at the code.
number+3 is the address of one-past the last element of array numbers.

Note: number + 3 is not equivalent to numbers[3]

numbers[3] is equivalent to *(numbers + 3)

i think you should read up on Pointer Arithmetic
1
2
  int numbers[] = {10,20,30};
  increment_all (numbers,numbers+3);




Im particularly confused about the (numbers+3) argument. That would make it the numbers[3] element in the array which doesn't even exist. So how is it being passed as an argument?

No that would make it the third element in the array and that would be numbers[2], not numbers[3]. Remember arrays start at zero and end at size - 1. So your valid array indexes would be 0, 1, 2.

number+3 is the address of one-past the last element of array numbers.


Nor sure I understand what you mean here...

How can you have address of an element past the last element? Thats what im confused about. How does numbers + 3 even exist?

I've already read Pointer Arithmetic.

No that would make it the third element in the array and that would be numbers[2], not numbers[3].


Thats not what bazetwo is saying.


A little more elaboration would be nice please.
Last edited on
How can you have address of an element past the last element? Thats what im confused about. Hows does numbers + 3 even exist?

in c++, it is legal to point to an element in an array or one past the end of an array. NOWHERE ELSE
But it is undefined behavior to try to dereference this address as it points to a memory that does not exist.

it is mainly used as a control value in transversing arrays.

so in your case,
1
2
3
4
5
6
7
8
9
10
 
numbers + 0 ==> you can take, dereference this address
numbers + 1 ==> you can take, dereference this address
numbers + 2 ==> you can take, dereference this address 
// one past the end
numbers + 3 ==> you can only take, but do not dereference this address
// 2 past the end
numbers + 4 ==> INVALID
// three past the end
numbers + 5 ==> INVALID


Last edited on
The standard library containers have iterators that in some ways resemble pointers.
Do you find the following code equally strange?
1
2
3
4
5
6
7
8
void print_all ( const std::vector<int> & arr )
{
  auto current = arr.begin();
  while ( current != arr.end() ) {
    std::cout << *current << '\n';
    ++current;
  }
}

I.e. what does the std::vector::end() do?

Many standard library algorithms take "begin" and "end", just like your functions.
Thank you bazetwo.

So to conclude, numbers +3 is equivalent to &numbers[3], correct?
Last edited on
Thanks, but I think I already understand that now.

I even replaced line 25 of the code with (&numbers[0],&numbers[3]); and it still works fine.

Topic archived. No new replies allowed.