Pointer Arithmatic Issue

I'm trying to remember how to do pointer arithmetic (I haven't coded in a while). This is what I came up with.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>

int main()
{
	int pow, pow2, pow3;
	int *dow = &pow;

	pow = 1;
	pow2 = 2;
	pow3 = 3;

	for(int i = 0; i < 3; i++)
	{
		std::cout << *dow << std::endl;
		++dow;
	}

	return 0;
}


When I run this the result is:
1
1
2
After I changed it to i < 4 it displayed 3 as expected but I can't figure out why pow's value repeats at the beginning. Thank you advance.
What you have here is undefined behavior. This program could do anything and be compliant with the standard.

1
2
3
4
5
6
7
8
9
#include <iostream>

int main() {
    int values[3] = { 1, 2, 3 };
    int* val = values;

    for (unsigned i=0; i<3; ++i)
        std::cout << *val++ << '\n' ;
}
Thank you for pointing that out, sorry about the trouble.
Topic archived. No new replies allowed.