PDA

View Full Version : Invalid conversion from ‘char*’ to ‘char’



Atomic_Sheep
25th September 2014, 02:01
I've got the following code which doesn't compile because "invalid conversion from ‘char*’ to ‘char’".


char HelloWorld::ConvertIntToChar(int iIntToConvert)
{
char cText[20];
sprintf("%d", cText);
return cText;
}

Any suggestions?

anda_skoa
25th September 2014, 10:57
cText is an array of char, also representable as char*.
Your function's return value is a single char.

If you want to return the first character in the array, write


return cText[0];


BTW, your sprintf() is wrong, the target buffer comes first, then the format string then the arguments specified in the format string.

Cheers,
_

wysota
26th September 2014, 00:05
I've got the following code which doesn't compile because "invalid conversion from ‘char*’ to ‘char’".


char HelloWorld::ConvertIntToChar(int iIntToConvert)
{
char cText[20];
sprintf("%d", cText);
return cText;
}

Any suggestions?

Hmm... in your opinion, does the code of the function have any sense? What do you think should happen when the function is executed?

Atomic_Sheep
26th September 2014, 09:02
I think I get it, iIntToConvert doesn't get used anywhere, so nothing should happen.

kaufenpreis
16th January 2015, 11:20
I've got the following code which doesn't compile because "invalid conversion from ‘char*’ to ‘char’".


char HelloWorld::ConvertIntToChar(int iIntToConvert)
{
char cText[20];
sprintf("%d", cText);
return cText;
}

Any suggestions?

There are lots of things wrong with this code. But since you haven't said what it's supposed to do, I will have to deduce what you intended it to do: return a string containing the decimal representation of the integer argument. The first problem is it doesn't return a string, it returns a single byte (single character). If we fix that in the old C style by returning a pointer to char, then the next problem is that we're returning a pointer to a local variable which will no longer exist after the function has returned, which is undefined behaviour; the program could do anything. We can fix that by either making cText static (but then subsequent calls will overwrite the same string returned by previous calls) or letting the caller pass in the pointer to the memory where they want to put the string (which doesn't solve the problem, it just foists the problem onto the caller). If we do one of those things then the program still has undefined behaviour, because the arguments to sprintf are in the wrong order (in any other compiler this would be an error, but treating a string literal as a non-const pointer is a Microsoft extension). If we fix that then the program still has undefined behaviour because the wrong number of arguments for the format string are passed to sprintf, iIntToConvert should be the third argument. If we fix that then finally we have a function which does what it's supposed to, with some caveats. Where on Earth did you find this code?

The moral is, don't use old C style code unless you're forced to, and if you're forced to by an outdated API, do it only at the last possible moment (e.g. use .c_str() to convert a std::string to a char array). It's far too easy to make mistakes. Use "new" C++ features (new meaning 16 years old in this case) like std::string which are much easier to understand and harder to misuse.


std::string HelloWorld::ConvertIntToString(int iIntToConvert)
{
std::stringstream ss;
ss << iIntToConvert;
return ss.str();
}
With the even more new (3 years old) C++11 this gets even easier:


std::string HelloWorld::ConvertIntToString(int iIntToConvert)
{
return std::to_string(iIntToConvert);
}