Results 1 to 5 of 5

Thread: Passing a (char) to a (int) argument - Is it safe?

  1. #1
    Join Date
    Sep 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Passing a (char) to a (int) argument - Is it safe?

    Hello everyone,

    I have a function in my program which receives (int)s from 0 to 9 , so it has a (int) arg, but I would like it to be able to recognize the '.' character. So what I'm doing is, just casting to (int) before passing it on to the function and inside it, casting it again to char and comparing against '.' to see if it's true. So that's how my code's like:
    Qt Code:
    1. myfunction( (int)'.' );
    2.  
    3. void myfunction( int value){
    4. char aux = (char)value;
    5.  
    6. if(aux == '.')
    7. cout << "TRUE";
    8. else
    9. cout << "Number : " << value;
    10.  
    11.  
    12. }
    To copy to clipboard, switch view to plain text mode 

    Well, it works, it correctly recons that i'm passing '.' and do the actions I expect it to do. It also recognizes the digits.

    I'm just wondering wether it is safe or not to do that, because it doesn't seem very elegant. Is it possible that in another architecture,compiler,operation system,whatever, it won't work out right?

    Thanks in advance,

    Luiz.

  2. #2
    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: Passing a (char) to a (int) argument - Is it safe?

    Yes, this is safe. As far as I remember you shouldn't even need to do the cast.
    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.


  3. #3
    Join Date
    Oct 2010
    Location
    Berlin, Germany
    Posts
    358
    Thanks
    18
    Thanked 68 Times in 66 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Passing a (char) to a (int) argument - Is it safe?

    what do you want to achieve? maybe there's a better solution...

  4. #4
    Join Date
    Sep 2011
    Posts
    7
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Passing a (char) to a (int) argument - Is it safe?

    Yes, there was a much better solution for my case. I just wanted to know if it could be ambiguous somehow.
    Thank you very much mates.

    Luiz.

  5. #5
    Join Date
    Apr 2010
    Posts
    769
    Thanks
    1
    Thanked 94 Times in 86 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Passing a (char) to a (int) argument - Is it safe?

    Whether it's safe or not depends on what you are trying to do. One potential problem, if you're trying to pick apart something like floating point numbers, is that decimal points aren't always used as the character for digit grouping. Commas are also seen, in which case the thousands separator is switched to a decimal point. (Of course, it isn't a "decimal point" anymore, but...)

    Many toolkits provide an abstraction for this character to avoid ambiguity. I don't know if Qt does this, but I wouldn't be surprised. If so, you should use that pseudo-character for comparisons, as it will change depending on the user's localization.

Similar Threads

  1. onclick event passing argument to js function
    By dyngoman in forum Qt Programming
    Replies: 0
    Last Post: 10th September 2011, 13:51
  2. Replies: 2
    Last Post: 11th August 2011, 15:42
  3. Replies: 2
    Last Post: 22nd June 2010, 21:35
  4. Trouble passing an argument
    By benlyboy in forum Newbie
    Replies: 5
    Last Post: 9th May 2010, 22:49
  5. QtScript : passing array as argument into function
    By derek_r in forum Qt Programming
    Replies: 4
    Last Post: 27th October 2007, 10:46

Tags for this Thread

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.