In this context, 0
is a null pointer constant. It can be converted, with or without a cast, to any pointer type, giving a null pointer of that type. In modern C++, you can write nullptr
to make it more clear that you mean a null pointer, not the number zero. There’s also a NULL
macro for the same purpose.
So here you have two pointers: one points to the string literal’s array of characters, the other is null, and doesn’t point to anything.
Dereferencing a null pointer gives undefined behaviour; typically, a segmentation fault, as you encountered.
Note that, in modern C++, you’ll need const char *
in order to point to the constant string literal "hello"
. Older versions of the language allowed the dangerous conversion to a non-const char *
for compatibility with ancient C code that didn’t have a concept of const
; that nastiness has now finally been removed from the language.
solved What is the result of casting in C++?