Thread: What dose this type casting mean??

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    4

    Post What dose this type casting mean??

    hi all:
    I just met a peice of code using such a type casting :
    Code:
    	if(to->sin_addr.s_addr != (unsigned)-1){
    		strcpy(hnamebuf, av[0]);
    		hostname = hnamebuf;
    	}
    how to understand this.

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    type casting allows you to convert from one type to another.
    In this case you're changing the way the compiler interprets the -1, which is a signed int by default. You're instructing the compiler to treat it as an unsigned value. i.e. as the largest value an int can hold.
    I'm guessing that in an error case all bits of s_addr are set to 1, which is is equivalent to -1 for an unsigned int.

    QuantumPete
    Last edited by QuantumPete; 06-11-2008 at 04:37 AM. Reason: Removed those poor singed ints.
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    4
    thank you QuantumPete!

  4. #4
    Registered User
    Join Date
    May 2008
    Posts
    87
    You're instructing the compiler to treat it as an unsigned value. i.e. as the largest value an int can hold.
    This is assuming a two's compliment number system right? So it may not be entirely portable, although should work on anything built since I started writing code.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by jason_m View Post
    This is assuming a two's compliment number system right? So it may not be entirely portable, although should work on anything built since I started writing code.
    Yes it does. And yes it should work on anything that isn't about to get a government old-age pension... ;-) [Although, if the spec defines that the value is -1 under error condition, rather than all ones, then it's fine even on a ones complement machine].

    And I think the cast is there to silence a compiler warning rather than anything else. The warning could also be bypassad by suffixing the number with a U.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Advantages of c++ type casting over c type casting
    By kaibalya2008 in forum C++ Programming
    Replies: 10
    Last Post: 05-05-2009, 11:09 AM
  2. type casting void *
    By Alexpo in forum C Programming
    Replies: 5
    Last Post: 06-23-2008, 03:05 AM
  3. Replies: 0
    Last Post: 03-20-2008, 07:59 AM
  4. Erros in Utility Header File
    By silk.odyssey in forum C++ Programming
    Replies: 4
    Last Post: 12-22-2003, 06:17 AM
  5. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM

Tags for this Thread