#include <iostream> using std::cout; using std::endl; int main(int argc, char ** argv) { int ia[10]; int ib[10]; int * pia = ia; int * pib = ib; cout << (pib - pia) << endl; /* тут выведет на экран 12, почему не 10? */ double da[10]; double db[10]; double * pda = da; double * pdb = db; cout << (pdb - pda) << endl; /* а тут 10, как и должно быть */ return 0; } I always thought that subtracting pointers of the same type would result in a count of elements between them, but in this case the int is wrong. Why?
Why, by the way, you can not subtract different types of pointers and display the number of bytes between them?
And the last question. As I know, the maximum addresses are at the beginning of the stack, and the end is in the code segment, the stack itself grows from top to bottom, i.e. in order of decreasing addresses, then why if you declare a lok on the stack. change. int a;int b; then their addresses will be: &a < &b , and not vice versa?
pib-pia,pdb-pdaoperations is undefined, it may depend on the compiler, optimization options and the phase of the moon. - andy.37