How can I print the address of first character?

后端 未结 1 1207
攒了一身酷
攒了一身酷 2021-01-02 23:47

Why the entire string is displayed as outcome? Why address of 1st character not getting printed? How can I print address of 1st character? Please help me.

#         


        
相关标签:
1条回答
  • 2021-01-03 00:19

    The << operator on std::cout will treat a char* as a null-terminated string. You will need to cast it to a void* to print the pointer value.

    Try this:

    #include <iostream>
    
    int main()
    {
        char x[6] = "hello";
        std::cout << static_cast<void*>(x);
    }
    
    0 讨论(0)
提交回复
热议问题