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

孤者浪人 提交于 2020-05-25 07:02:34

问题


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

cout << &charArray

gives me a valid address, However if I try to get the address of a specific element, it spits out garbage:

cout << &charArray[0]

回答1:


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.




回答2:


You can do something like

&charArray + index * sizeof(char)


来源:https://stackoverflow.com/questions/9519749/how-do-i-get-the-address-of-elements-in-a-char-array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!