Struct help...

This is a discussion on Struct help... within the C Programming forums, part of the General Programming Boards category; Hello World! (struct sockaddr *)&my_addr What does this line do?... I am still trying to learn C...

  1. #1
    Registered User
    Join Date
    Jul 2008
    Location
    Denmark
    Posts
    22

    Talking Struct help...

    Hello World!

    (struct sockaddr *)&my_addr

    What does this line do?... I am still trying to learn C

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    It casts the address of my_addr to type struct sockaddr *. By itself, it's useless.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    It's for network programming. If you're interested in learning more, I suggest you read Beej's network tutorial. http://beej.us/guide/bgnet/

    It's rather advanced stuff, though. Don't try it until you have some experience with C.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Registered User
    Join Date
    Jul 2008
    Location
    Denmark
    Posts
    22
    Quote Originally Posted by MacGyver View Post
    It casts the address of my_addr to type struct sockaddr *. By itself, it's useless.
    So it takes the address of my_addr and place it in the structure sockaddr... Am i right?

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    No, you're wrong.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    (struct sockaddr*)&my_addr

    Green = Tells the compiler it's a cast
    Red = Tells the compiler what you're casting to
    Blue = Address of operator: takes the address of a variable
    Orange = Tells the compiler what you want to take the address of

    The cast converts the expression on the right to the type specified.
    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.

  7. #7
    Registered User
    Join Date
    Jul 2008
    Location
    Denmark
    Posts
    22
    Thx alot! Elysia

  8. #8
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    A bit of an advanced concept for just starting out learning C. If you are interested in network programming. "Unix Network Programming - The sockets network API" is a good read.

  9. #9
    Registered User
    Join Date
    Jul 2008
    Location
    Denmark
    Posts
    22
    Quote Originally Posted by MacGyver View Post
    It casts the address of my_addr to type struct sockaddr *. By itself, it's useless.
    I THINK i get it now, it makes &my_addr act like struct sockaddr * , but how can a memory address act like a structure ?

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    The thing is that your struct is just a block of raw memory in the eyes of the CPU or the computer.
    The compiler just manages that memory for you.
    And because memory is "raw," there are actually no "types". Your struct is just a piece of memory. The compiler keeps track of its type.
    But you can tell the compiler that it's another type than it is. It works because it's just a block of memory.
    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.

  11. #11
    Registered User
    Join Date
    Jul 2008
    Location
    Denmark
    Posts
    22
    Quote Originally Posted by Elysia View Post
    The thing is that your struct is just a block of raw memory in the eyes of the CPU or the computer.
    The compiler just manages that memory for you.
    And because memory is "raw," there are actually no "types". Your struct is just a piece of memory. The compiler keeps track of its type.
    But you can tell the compiler that it's another type than it is. It works because it's just a block of memory.
    So i places the value of &my_addr in the struct sockaddr memory block.... right? and whats i the "*" good for?

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Again, because it's a pointer.
    It simply tells the compiler to treat the data at the address in the pointer as something else.
    There's a difference:

    Code:
    float f = 1.0f;
    int n = (int)f; /* Converts the data inside f to an integer - result is 1. */
    int* pN = (int*)&f; /* Tells the compiler to treat the data at the address where f resides as 
    an int. The result will not be 1, but something much else. */
    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.

  13. #13
    Registered User
    Join Date
    Jul 2008
    Location
    Denmark
    Posts
    22
    Quote Originally Posted by Elysia View Post
    Again, because it's a pointer.
    It simply tells the compiler to treat the data at the address in the pointer as something else.
    There's a difference:

    Code:
    float f = 1.0f;
    int n = (int)f; /* Converts the data inside f to an integer - result is 1. */
    int* pN = (int*)&f; /* Tells the compiler to treat the data at the address where f resides as 
    an int. The result will not be 1, but something much else. */
    Why is the result not 1?...

  14. #14
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Look, in all honesty, you're asking questions for things that require way more knowledge than you have at the moment and will likely have for some time. You need to buckle down and get a book or a tutorial and start from the basics and work your way up.

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Quote Originally Posted by MKirstensen View Post
    Why is the result not 1?...
    Because, simply put, floats are not stored the same way ints are. That's why.
    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.

Page 1 of 2 12 LastLast
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

Tags for this Thread


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