Thread: why a struct?

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    596

    why a struct?

    I've just started reading some material on sockets programming. These definitions in include/netinet/in.h caught my attention
    Code:
    typedef uint32_t in_addr_t;
    struct in_addr
      {
        in_addr_t s_addr;
      };
    and I'm wondering why they didn't simply define
    Code:
    typedef uint32_t in_addr;
    ?

    What's the point of a struct consisting of just a single member?

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Type safety.

    It prevents an in_addr from being assigned any old number. Combined with a special creation function for in_addr objects, the programmer using the librairy does not even need to know that a in_addr is an 32 bit integer under the hood.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    I don't get it. What prevents a programmer from assigning
    Code:
    in_addr.s_addr = 15;

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by R.Stiltskin
    What prevents a programmer from assigning
    The intelligence to use the interface instead of relying on implementation detail?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Ouch!

    OK, first I'll learn the interface, then I'll ask the questions.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by R.Stiltskin
    Ouch!
    Hehe, I see how it can be read now. Nah, I was not trying to insult your intelligence
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by R.Stiltskin View Post
    I don't get it. What prevents a programmer from assigning
    Code:
    in_addr.s_addr = 15;
    Nothing. But it won't happen implicitly, and is less likely to be done accidentally.

    For example, if you have a function that takes an in_addr, as the first arguement, and an int the second, if a programmer were to try to pass the arguments in the wrong order, it would lead to a compile time error.

    It also allows a library developer to later change the member(s) of the struct, while being sure that the client code will still work, because the client does not directly need to access the members of the struct.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-03-2008, 03:10 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. Replies: 10
    Last Post: 05-18-2006, 11:23 PM
  4. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 09:57 AM
  5. Tutorial review
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 03-22-2004, 09:40 PM