0

Currently, I'm trying to understand why I have to use:

std::cout << "&str1:" << &str1 << std::endl;

To get the memory address of:

const char * str1 = "Good Morning";

I expected that:

std::cout << "str1:" << str1 << std::endl;

would do so.

Here is an example:

void charPointerExamples() {
    const int * myInt = new int(1);
    const char * str1 = "Good Morning";
    const char * str2 = str1;
    std::cout << "myInt:" << myInt << std::endl;
    std::cout << "&str1:" << &str1 << std::endl;
    std::cout << "&str2:" << &str2 << std::endl;

    std::cout << "str1:" << str1 << std::endl;
    std::cout << "str2:" << str2 << std::endl;
    
    std::cout << "*str1:" << *str1 << std::endl;
    std::cout << "*str2:" << *str2 << std::endl;
    delete myInt;
}

The output itself:

    myInt:0081D450
    &str1:003CFD20
    &str2:003CFD14
    str1:Good Morning
    str2:Good Morning
    *str1:G
    *str2:G
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
  • 2
    you are not printing the adress of the string, but the adress of the pointer that points to the string. The things is that using `std::cout` with a `char *` will not print the adress. Looking for a dupe... – 463035818_is_not_an_ai Aug 26 '20 at 08:04
  • @Bathsheba thanks for sharing. may you can tell me where i have to look to get this information on my own next time before i ask? – SirRollalot Aug 26 '20 at 08:10
  • 1
    @idclev463035818: True, and I can't edit the comment. So I'll tin it. – Bathsheba Aug 26 '20 at 08:21

0 Answers0