0

The following program has some peculiar results regarding the evaluation of 0x787619ce51b4 - 0x787619ce51b0 or equivalently &x[1] - &x[0].

What is causing this behavior?

#include <iostream>
#include <string>

int main()
{
    int x[3] = {10, 20, 30};

    // Here we have the array values
    std::cout << x[0] << "\n";              // 10
    std::cout << x[1] << "\n";              // 20
    std::cout << x[2] << "\n";              // 30

    // Here we have the array addresses
    std::cout << &x[0] << "\n";             // 0x787619ce51b0
    std::cout << &x[1] << "\n";             // 0x787619ce51b4
    std::cout << &x[2] << "\n";             // 0x787619ce51b8

    // Here we have the math results
    std::cout << &x[0] - &x[0] << "\n";     // 0
    std::cout << &x[1] - &x[0] << "\n";     // 1
    std::cout << &x[2] - &x[0] << "\n";     // 2

    // How come we get the result (1) when we do (&x[1] - &x[0])
}
AlanSTACK
  • 5,525
  • 3
  • 40
  • 99
  • 2
    You have to remember that pointer arithmetic results in values in multiples of the size of the *base type*. So for `int *` the base type is `int`, which usually is 32 bits (i.e. 4 bytes). – Some programmer dude Oct 02 '17 at 08:08
  • @Someprogrammerdude what is the type of `&x[1] - &x[0]`? Is it `int` or is it `int*`. Also in c++, how can I print the type of a variable for debugging? – AlanSTACK Oct 02 '17 at 08:10
  • If you subtract two pointers, the result is of the type [`std::ptrdiff_t`](http://en.cppreference.com/w/cpp/types/ptrdiff_t). – Some programmer dude Oct 02 '17 at 08:12
  • 2
    The type of `&x[1] - &x[0]` is of type `std::ptrdiff_t`, which is a signed integral type capable of holding any valid result of subtracting two pointers. – Peter Oct 02 '17 at 08:12
  • in php there is a function called `printr` that prints the variables values formatted, and the variable type. Is there such a thing like this for c++? Or am I supposed to remember (`std::ptrdiff_t`) off the top of my head? – AlanSTACK Oct 02 '17 at 08:13
  • 2
    And you can't reliably print the name of a type, since C++ have no [introspection](https://en.wikipedia.org/wiki/Type_introspection). There *is* the [`typeid` expression](http://en.cppreference.com/w/cpp/language/typeid) which gives you a [`std::type_info`](http://en.cppreference.com/w/cpp/types/type_info) structure which have an [*implementation defined* name](http://en.cppreference.com/w/cpp/types/type_info/name). – Some programmer dude Oct 02 '17 at 08:14
  • @Alan - You kinda do. Especially since `std::ptrdiff_t` is a type alias. It stands for some other native type. So even if there was introsepction, it probably wouldn't say `std::ptrdiff_t`. – StoryTeller - Unslander Monica Oct 02 '17 at 08:16
  • And while the `std::ptrdiff_t` thing could be found out through a little searching, most who needs to know what the type of a pointer-to-pointer subtraction is *do* know the type by heart. – Some programmer dude Oct 02 '17 at 08:17
  • Also, there's a semi-useful trick you can use to get a printout of the type of an expression. [It involves `dectlype` and an undefined template](https://ideone.com/BAD2XA). – StoryTeller - Unslander Monica Oct 02 '17 at 08:17
  • @StoryTeller Sorry for sounding stupid. But when I tried to run your code it says `error: aggregate 'WhatType wt' has incomplete type and cannot be defined` – AlanSTACK Oct 02 '17 at 08:28
  • 2
    @Alan - Your'e not stupid. It's just counter intuitive. The type name is in the error message. – StoryTeller - Unslander Monica Oct 02 '17 at 08:29
  • @StoryTeller I see it now. Thanks ! – AlanSTACK Oct 02 '17 at 08:30

0 Answers0