Thread: Code sippet needs explaining

  1. #1
    Join Date
    Sep 2003

    Code sippet needs explaining


    I have just started socket programming.

    I am having trouble understanding this line of code.

    bind(int socket, struct sockaddr *address, int address_length);
    I am using it like this:
    bind(s, (struct sockaddr *)&sin, sizeof(sin));
    I can understand the 1st and 3rd parameters, but can't understand the second.

    (struct sockaddr *) &sin
    My idea is that it is passing the &sin by reference and casting it to a pointer to a struct of sockaddr.

    Correct me if I am wrong,

    Any examples would be good for me.

    Many thanks for your help,


  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    You are correct. This is because "sockaddr" is a generic form, and your "sin" is (I guess) of type "sockaddr_in", so the compiler would complain if you didn't have the cast.

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Sydney, Australia
    You are correct that it's casting it to a pointer to struct sockaddr, but C does not have "pass by reference". You are passing a pointer to sin.

  4. #4
    Join Date
    Sep 2003

    Thanks for your response.

    but C does not have "pass by reference"
    Does this mean that you can use references in your code but you cannot pass them by reference in your functions.

    Many thanks.


  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Does this mean that you can use references in your code but you cannot pass them by reference in your functions.
    It depends on what is meant by "reference". C does not have C++ style references. C can simulate pass by reference using pointers.
    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

  6. #6
    Registered User
    Join Date
    Jan 2008
    I think it helps to read '&' as "address of" or "pointer to" in C.
    &sin would be read "the address of sin"
    (struct sockaddr *)&sin would be read "the address of sin cast as a pointer to a sockaddr structure"

  7. #7
    Join Date
    Oct 2007
    Inside my computer
    I agree. I find it better to separate the two terms. References are C++ only.
    Another thing one might note is that it requires a cast since C doesn't support classes and polymorphism either. In C++, it would be easy to pass it without a cast. But this is C
    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. Proposal: Code colouring
    By Perspective in forum A Brief History of
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM