• Advertisement
Sign in to follow this  

invalid conversion from 'char' to 'const char*'(using strings)

This topic is 2103 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

How can I send a single character from one string into a function parameter of type string to be equal to another string.
For example:

/// The function (thisString is of type string)
void setChar(string c)
{
thisString = c;
}

/// Calling the function
string alphabet = "abcdefg";
setChar(alphabet[0]);


When I do this, i get the error Invalid conversion from 'char' to 'const char*'. Also, no I cannot change the types in the function to char.

Share this post


Link to post
Share on other sites
Advertisement
You can also do:

setChar(std::string(1, alphabet[0])); // creates a string of length 1 consisting of alphabet[0] repeated 1 time
setChar(std::string(alphabet, 0, 1)); // creates a string of length 1 from string alphabet starting from the index 0

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement