Union in a struct?

This is a discussion on Union in a struct? within the C Programming forums, part of the General Programming Boards category; Ok, I'm learning Winsock, but there's this part I'm confused about, with a union in a struct (I know how ...

  1. #1
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879

    Union in a struct?

    Ok, I'm learning Winsock, but there's this part I'm confused about, with a union in a struct (I know how it works but I don't know why it's done like that). This is what it looks like:
    Code:
    struct in_addr
    {
            union
            {
                    struct { u_char s_b1,s_b2,s_b3,s_b4; } S_un_b;
                    struct { u_short s_w1,s_w2; } S_un_w;
                    u_long S_addr;
            } S_un;
    };
    And the only member of in_addr is the union S_un. What would the point of this be? Wouldn't it be easier and simpler just to use the union without embedding it in a struct?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    In a word: compatiblity. This is basicly just the Windows implementation of BSD sockets.

    Like so:
    Code:
    struct in_addr {
       u_long s_addr;
    };
    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Hmm, then in this case, what would the point of having the u_long in the struct be?? Wouldn't it be more practical just to use a u_long variable instead?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Converting from C to C++
    By Taka in forum C++ Programming
    Replies: 5
    Last Post: 04-08-2009, 03:16 AM
  2. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  3. Looking for a way to store listbox data
    By Welder in forum C Programming
    Replies: 20
    Last Post: 11-02-2007, 12:48 AM
  4. What's wrong with my search program?
    By sherwi in forum C Programming
    Replies: 5
    Last Post: 04-28-2006, 10:57 AM
  5. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM

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