Question about pointers/address addition


If I have
1
2
double a[10] = {10.0, 11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0};
double* ptr = a + 1; //Talking about this line, the a + 1. 

Why does the +1 in a + 1 actually increment the pointer by the size of a double instead of just 1? Is it overloaded?


Am I correct in saying that it doesn't matter if each double value stores more bytes than an int value, the "+ 1" still increments the address by the size of the each element itself?

So for example, if I had a pointer to a really_big_type, doing
1
2
3
really_big_type a[2] = {32, 42};
really_big_type* ptr = a + 1;
std::cout << *ptr;

The print statement should still print 42?
Last edited on
Yes.

You don't simply declare a pointer. You declare a pointer to type really_big_type. The compiler can thus make use of that info. It can multiply your 1 with sizeof(really_big_type) when calculating an address.
Alright, thanks for the quick reply.
Topic archived. No new replies allowed.