cout << with char* argument prints string, not pointer value

后端 未结 6 1315
旧时难觅i
旧时难觅i 2020-11-21 07:39

This:

const char * terry = \"hello\";
cout<

prints hello instead of the memory address of the \'h\'.

6条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-21 08:05

    "hello" is a string, i.e. the char array. const char* is a pointer to this array, so when you dereference this pointer, you get the value of the first element.

    It is like if you have

    int a[] = {1, 2, 3};
    int *b = a;
    cout << *b << endl;
    

    you get just 1 printed.

提交回复
热议问题