Results 1 to 5 of 5

Thread: Invalid conversion from ‘char*’ to ‘char’

  1. #1
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Invalid conversion from ‘char*’ to ‘char’

    I've got the following code which doesn't compile because "invalid conversion from ‘char*’ to ‘char’".

    Qt Code:
    1. char HelloWorld::ConvertIntToChar(int iIntToConvert)
    2. {
    3. char cText[20];
    4. sprintf("%d", cText);
    5. return cText;
    6. }
    To copy to clipboard, switch view to plain text mode 

    Any suggestions?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Invalid conversion from ‘char*’ to ‘char’

    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
    Qt Code:
    1. return cText[0];
    To copy to clipboard, switch view to plain text mode 

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

    Cheers,
    _

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Invalid conversion from ‘char*’ to ‘char’

    Quote Originally Posted by Atomic_Sheep View Post
    I've got the following code which doesn't compile because "invalid conversion from ‘char*’ to ‘char’".

    Qt Code:
    1. char HelloWorld::ConvertIntToChar(int iIntToConvert)
    2. {
    3. char cText[20];
    4. sprintf("%d", cText);
    5. return cText;
    6. }
    To copy to clipboard, switch view to plain text mode 

    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?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. #4
    Join Date
    Jun 2011
    Posts
    203
    Thanks
    7
    Thanked 4 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Invalid conversion from ‘char*’ to ‘char’

    I think I get it, iIntToConvert doesn't get used anywhere, so nothing should happen.

  5. #5
    Join Date
    Nov 2014
    Posts
    32
    Thanked 1 Time in 1 Post
    Qt products
    Platforms
    Windows

    Default Re: Invalid conversion from ‘char*’ to ‘char’

    Quote Originally Posted by Atomic_Sheep View Post
    I've got the following code which doesn't compile because "invalid conversion from ‘char*’ to ‘char’".

    Qt Code:
    1. char HelloWorld::ConvertIntToChar(int iIntToConvert)
    2. {
    3. char cText[20];
    4. sprintf("%d", cText);
    5. return cText;
    6. }
    To copy to clipboard, switch view to plain text mode 

    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.
    Qt Code:
    1. std::string HelloWorld::ConvertIntToString(int iIntToConvert)
    2. {
    3. std::stringstream ss;
    4. ss << iIntToConvert;
    5. return ss.str();
    6. }
    To copy to clipboard, switch view to plain text mode 
    With the even more new (3 years old) C++11 this gets even easier:
    Qt Code:
    1. std::string HelloWorld::ConvertIntToString(int iIntToConvert)
    2. {
    3. return std::to_string(iIntToConvert);
    4. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 4
    Last Post: 18th May 2012, 06:04
  2. Invalid conversion error while Storing into Combobox
    By owais_blore in forum Qt Programming
    Replies: 1
    Last Post: 20th February 2012, 16:08
  3. int to char conversion
    By cooper in forum Newbie
    Replies: 3
    Last Post: 14th June 2011, 03:50
  4. Problem in conversion of QChar to char
    By Prashant_Bhutani in forum Newbie
    Replies: 5
    Last Post: 19th April 2010, 10:27
  5. char* to QString: conversion
    By abghosh in forum Qt Programming
    Replies: 9
    Last Post: 8th March 2010, 09:32

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.