How to get the address of elements in a char array?

I have an array charand 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 spills out garbage:

cout << &charArray[0]
+5
source share
2 answers
std::cout << (void*) &charArray[0];

There is an overload operator<<for here char*that tries to print a zero-terminated string that, in its opinion, the pointer points to the first character. But not all char arrays are nul-terminated strings, so garbage.

+6
source

You can do something like

&charArray + index * sizeof(char)
+1
source

All Articles