Thread: C De-reference + Memory address together in struct?

  1. #1
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507

    C De-reference + Memory address together in struct?

    Hello. I have my head (I think) around basic pointers and such fairly cleanly. Im able to make my own linked lists and stacks and queues with no problem. However when looking at a piece of code for a server in C i was fouled up at this line (and ive seen its likes before).

    their_addr.sin_addr = *((struct in_addr *)he->h_addr);

    I understand something is being inputed into the structure element, what I dont know. I dont understand the multiple * in seemingly random places (Help please or an explanation). What does the * on the outside do? And the * on the inside? Thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Start with he. Cast it (that's what "(struct in_addr *)" does), so that we now think that it has an h_addr member. Use -> to to dereference the pointer he, and grab the h_addr member variable (since it ends in "addr", I'd bet it's a pointer too). Then get the contents pointed to by h_addr with the * around the whole glob.

  3. #3
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Ahh ok, so cast seems to be the big thing here? I haven't encountered them before...I suppose thats why I had no idea whats going on. Thanks ill go read up on them now

  4. #4
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Quick question then i suppose. A cast is a way of transforming something into another type? So in this case he->h_addr turns from whatever type it is and into a struct in_addr? And then that dereferenced value is stored? O.o

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by valaris View Post
    Quick question then i suppose. A cast is a way of transforming something into another type? So in this case he->h_addr turns from whatever type it is and into a struct in_addr? And then that dereferenced value is stored? O.o
    he->h_addr turns from whatever type it is and turns into a pointer to a struct in_addr.

  6. #6
    Registered User valaris's Avatar
    Join Date
    Jun 2008
    Location
    RING 0
    Posts
    507
    Ahh i see Much thanks, makes sense now!

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Actually, you should beware of casts. In C, there is no type information. Everything is raw and the type tells the compiler how the type should be treated. In effect, this means you can change the type to whatever and it will work (this especially holds true to pointers).
    HOWEVER, because you are merely changing how the compiler treats the type, it doesn't mean it's safe. You are not changing what type it actually is.

    What the line essentially does is:
    - tell the compiler that the pointer is not of whatever type it's now, instead it's a pointer to struct in_addr.
    - dereference pointer of type pointer to in_addr (the new type you told the compiler it was) and return the member h_addr.
    - dereference the member h_addr (this is what * does, remember?).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What does this do (Windows API)?
    By EVOEx in forum Windows Programming
    Replies: 4
    Last Post: 12-19-2008, 10:48 AM
  2. Assigning memory address of member struct to pointer.
    By Tronic in forum C++ Programming
    Replies: 2
    Last Post: 03-20-2004, 05:53 PM
  3. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  4. Is it necessary to write a specific memory manager ?
    By Morglum in forum Game Programming
    Replies: 18
    Last Post: 07-01-2002, 01:41 PM
  5. qt help
    By Unregistered in forum Linux Programming
    Replies: 1
    Last Post: 04-20-2002, 09:51 AM