samjoe samjoe - 2 months ago 21
C++ Question

Array address not visible

Refer the code below:

#include <iostream>

class Boy {
char name[10];
public:
void show() {
*name = 0;
std::cout << "\n" << &name[0];
}
};

int main() {
Boy b;
b.show();

}


Here, why don't we see the address of name[0]. I also tried with
name
, which itself is address. Still I can't see the address, it returns blank screen.

Answer

It's because you're using char* overload for operator<<, which treats the pointer as a pointer to c-string. Cast your pointer to void* to print it as such.

std::cout << "\n" << static_cast<void*>(&name[0]);