In reference to the code below: why does in one case we get the full string and in the other the address? Can someone please explain this?

char mary[] = "Mary";
char* aPtr = &mary[0];

int b = 5;
int* bPtr = &b;

cout << "aPtr value is " << aPtr << endl; // Prints "Mary"
cout << "bPtr value is " << bPtr << endl; // Prints address of integer b

Recommended Answers

All 2 Replies

It works differently for char. It does not print the address, but it prints all
characters that the pointer is pointing to, until it reaches a null character.

You can reinterpret_cast your char pointer to an int pointer or void pointer to print out its address.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.