Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Is " const wchar_t* " a pointer?

Normally when we write:

int a = 10;
int* ptr = &a;
std::cout << *ptr;

Code output is:

> 10

But when I write this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const wchar_t* str = L"This is a simple text!";
std::wcout << str << std::endl;
std::wcout << &str << std::endl;

Code output is:

> This is a simple text!
> 012FFC0C

So this makes me confused.

  • Doesn’t that Asterisk symbol stand for pointer?
  • If it is a pointer, how is it possible for us to assign a value other
    than the address value?
  • Shouldn’t the top output be at the bottom and
    the bottom output at the top?

>Solution :

L"This is a simple text!" is an array of type const wchar_t[23] containing all the string characters plus a terminating 0 char. Arrays can decay in which case they turn into a pointer to the first element in the array which is what happens in

const wchar_t* str = L"This is a simple text!";

std::wout can be used with a << operator that takes such a null terminated character as second operand; this operator prints the string.

The second print simply prints the address of the pointer, since there is no overload for the << operator handling an argument of type const wchar_t** differently to arbitrary pointer types (except for some specific pointer types as seen in the first print).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading