why a struct?

This is a discussion on why a struct? within the C Programming forums, part of the General Programming Boards category; 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; ...

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

    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,021
    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
    595
    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
    21,431
    Quote Originally Posted by R.Stiltskin
    What prevents a programmer from assigning
    The intelligence to use the interface instead of relying on implementation detail?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    595
    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
    21,431
    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
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  7. #7
    Registered User
    Join Date
    Apr 2006
    Posts
    2,021
    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, 02: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, 08:40 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21