-1

Why in my code I see value, which stored in the address, but not adress?

char *fortunes[] =
    {
        "1",
        "2",
        "3",
        "4"
    };

    cout << *fortunes[2];   // result 3
    cout << fortunes[2];    // result 3, but I expected to see adress
Amazing User
  • 3,473
  • 10
  • 36
  • 75

1 Answers1

4

There is an overload for std::ostream& operator<< which takes a const char* and interprets it as a null-terminated string, printing out characters until it finds the null terminator. If you want to print the value of the pointer (the address it holds), you can cast it to void*.

For example

cout << reinterpret_cast<const void*>(fortunes[2]) << endl;
juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • or to make it more obvious, as we use this overload all the time: `cout << "3";` - you wouldn't expect an address to be printed, would you? – Karoly Horvath Mar 11 '15 at 10:45