How do I get the address of elements in a char array?

后端 未结 2 551
慢半拍i
慢半拍i 2021-02-15 11:29

I have a char array and I need to get the address of each element.

cout << &charArray

gives me a valid address, However

相关标签:
2条回答
  • 2021-02-15 12:10

    You can do something like

    &charArray + index * sizeof(char)
    
    0 讨论(0)
  • 2021-02-15 12:31
    std::cout << (void*) &charArray[0];
    

    There's an overload of operator<< for char*, that tries to print the nul-terminated string that it thinks your pointer points to the first character of. But not all char arrays are nul-terminated strings, hence the garbage.

    0 讨论(0)
提交回复
热议问题