You need to keep in mind what a pointer is — it's just a normal variable that holds an address, much like a char
holds a character value. This address can be used to look up another variable (with the *
operator).
When you do char* pEmpty = new char
, you're giving pEmpty
the value returned by new char
, which is the address of a chunk of memory large enough to hold a char value. Then you use *pEmpty
to access this memory and assign it the char value 'x'
.
In the second example, you write pEmpty = 'x'
— but remember that pEmpty
is a pointer, which means it's supposed to hold an address. Is 'x'
an address? No, it's a character literal! So that line isn't really meaningful.
In the third example, you're assigning pEmpty
the string literal "x"
. Is this an address? Yes, it is. The literal evaluates to the address of that constant string.
Remember, pointers are a completely different thing from the type that they point to. They can be used to access a value of that type, but they are a completely different type of their own.