char *p = 's';
It gives out the error
cannot initialize a variable of type '
char *' with an rvalue of type 'char'
Can anyone explain it to me? Thanks a lot.
p is a pointer, of type char*. 's' is a character literal of type char. You can't initialise a pointer from a character.
Maybe you want p to be a single character:
char p = 's';
or maybe you want it to point to a string containing the character 's':
char const *p = "s"; // Must be const, since string literals are constant
you made a mistake. You should use double quotation(") instead of single quotation(').
Typically, 's' means a character literal and it is evaluated to type char.
while your p is a char type pointer(char*) initialization doesn't work.
use "s" to get char pointer. Not that is gives you a const char * and you can type case it to a char*.
enter code here
char* p = (char*)"s";
or
const char* p = "s"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With