samjoe samjoe - 10 months ago 79
C++ Question

Array address not visible

Refer the code below:

#include <iostream>

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

int main() {
Boy b;;


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

Answer Source

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]);