Sign in to follow this  

invalid conversion from const char*' to char'

This topic is 3870 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

"m" is a string literal. Like any literal, its value is constant.

In C and C++, character literals (as opposed to string literals) use single quotes: 'm'.

Share this post


Link to post
Share on other sites
It should be single quotes:
    answer[0]='m'; 
Double quotes signify a string, which is an array of chars. You are trying to assign a pointer to an array of char to a single char, and the compiler won't do that conversion (pointer to integer) automatically. "m" is a pointer-to-const because "m" is a literal and therefore constant.

If you want to copy "m" (or any string) to the array, you would do something like this:
    strcpy( answer, "m" ); 

Share this post


Link to post
Share on other sites
heh, i figured it out...

i was using " instead of '

(i still dont know why i'm uspposed to be using ', so if someone could explain it to me..)

it's always the little things that get ya...

Share this post


Link to post
Share on other sites
Quote:
Original post by finky45
(i still dont know why i'm uspposed to be using ', so if someone could explain it to me..)


double quotes are for strings. single quotes are for single chars.

"I am a string"
'm' <-- a char

The only confusing bit is that you can have strings of single characters:

"m" <-- a single character string.

A C-string is an array of chars in memory terminated by an "invisible" '\0' character (NULL terminated). That's how the algorithms like strlen and such know where the end of the string is in memory.

So "m" in memory actually looks like: m\0
While 'm' in memory is just: m

Basically, the problem with C-strings is that to understand how they actually work you need to have already studied the chapters on Pointers and Dynamic Memory Allocation. Those chapters tend to be at the end of the book, but C-strings appear at the beginning. So generally you're expected to use them but not understand them until the end of the book. =)

-me

Share this post


Link to post
Share on other sites
Because "m" is a string that just so happens to contain only one character. A string can consist of zero or more characters. ("" is a valid string, as is "m" and "cheese" or "hello world")
'm' is the character m. a character is only one single letter. ('m' is a character. '', 'cheese' or 'hello world' are not.)

In your code, answer is an array of characters. So the only thing you can assign to answer[0] is... a character.

Share this post


Link to post
Share on other sites

This topic is 3870 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this