0

I have a code snippet getting adress of double

#include <iostream>

class Vector {
public:
    Vector(int vecSize) :elem(new double[vecSize]), size(vecSize) {}
    double& operator[](int i) { return elem[i]; }
    int getSize() { return size; }
private:
    double* elem;
    int size;
};


int main()
{
    const int vecSize = 6;
    Vector myVec(vecSize);

    for (int i = 0; i < vecSize; i++)
    {
        std::cout << &myVec[i] << '\n';
    } 
}

can get the adresses of double values

000002336DE2E030
000002336DE2E038
000002336DE2E040
000002336DE2E048
000002336DE2E050
000002336DE2E058

in this code snippet when trying to get adresses of char values.

#include <iostream>

class Vector {
public:
    Vector(int vecSize) :elem(new char[vecSize]), size(vecSize) {}
    char& operator[](int i) { return elem[i]; }
    int getSize() { return size; }
private:
    char* elem;
    int size;
};


int main()
{
    const int vecSize = 6;
    Vector myVec(vecSize);

    for (int i = 0; i < vecSize; i++)
    {
        std::cout << &myVec[i] << '\n';
    } 
}

get result like this

══════²²²²
═════²²²²
════²²²²
═══²²²²
══²²²²
═²²²²

or something like this

══════²²²²D
═════²²²²D
════²²²²D
═══²²²²D
══²²²²D
═²²²²D

i try std::hex to convert, did not work

std::cout << std::hex << &myVec[i] << '\n';

Want to know what is the reason behind this ?

Mikel F
  • 3,567
  • 1
  • 21
  • 33
UPinar
  • 1,067
  • 1
  • 2
  • 16
  • Probably has something to do with char* being an old style string and being formatted as such. Perhaps casting the address? – Mikel F Jan 27 '23 at 18:07
  • 4
    The trouble is that the output stream is interpretting a `char*`as pointing at a string so it prints the string. You can get the address printed by converting the pointer to char to pointer to void. `std::cout << std::hex << ((void*)&myVec[i]) << '\n';` – Martin York Jan 27 '23 at 18:07
  • 2
    The vast, dominatingly vast, majority of the time when someone wants to print a pointer to a `char`, they have a null-terminated string they want to print. C++'s IO streams were designed to provide the least amount of surprise and do what most people will expect most of the time. – user4581301 Jan 27 '23 at 18:08
  • [https://stackoverflow.com/questions/4860788/why-is-address-of-char-data-not-displayed](https://stackoverflow.com/questions/4860788/why-is-address-of-char-data-not-displayed) – drescherjm Jan 27 '23 at 18:09
  • 1
    `std::hex` will print integers in hex format. Since the `char *` follows a different set of rules from the integer-printing rules, it can't help here. – user4581301 Jan 27 '23 at 18:11
  • @MartinYork it also works when i use `std::cout << std::hex << ((int*)&myVec[i]) << '\n';` – UPinar Jan 27 '23 at 18:13
  • 2
    @UPinar It will (probably) work for any type other than `char*`. For simple things like this it probably does not matter. But it is best not to cast to things that are not accurate. So by casting to `void*` you are saying this is a pointer to an unknown type and you can't misuse it accidentally. – Martin York Jan 27 '23 at 18:15
  • @MartinYork yes understand – UPinar Jan 27 '23 at 18:16

0 Answers0