Pointers for arrays

Hello everyone!

I've got this "pickle":
1
2
3
const int SIZE = 3;
int a[SIZE] = {1,12,43};
int *pa = a + 3;


Let's say that:

&a = 0x3fffd1a

what about pa? Is it 6 bytes after base address of a? (assuming that int here occupies 2bytes in memory)

Thanks for any help :)
Last edited on
pa would be pointing to the address of a[2].

Not so - it would be pointing to the address of a[3] (which technically is ouside the bounds of the array)
This is great because you can easily pass pointers to c-array elements to the new template algorithms of C++. Follow the link for an example of what the find algorithm would look like and you'll see how the pointers would be used within the algorithm in order to comply with the request. For this to work we need to be able to increment pointers in the same way that we would increment iterators in order to reach the next element. Also it just makes logical sense for pointers to behave that way.
std::find(a, a + SIZE, 12);
http://cplusplus.com/reference/algorithm/find/
Doh! you're right.

0+3 = 3 lol...............
Topic archived. No new replies allowed.