Code sippet needs explaining

This is a discussion on Code sippet needs explaining within the C Programming forums, part of the General Programming Boards category; Hello, I have just started socket programming. I am having trouble understanding this line of code. Code: bind(int socket, struct ...

  1. #1
    UK2
    Join Date
    Sep 2003
    Posts
    112

    Code sippet needs explaining

    Hello,

    I have just started socket programming.

    I am having trouble understanding this line of code.

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

    Code:
    (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,

    Steve

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

    --
    Mats
    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
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    868
    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
    UK2
    Join Date
    Sep 2003
    Posts
    112
    Hello,

    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.

    Steve

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,734
    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.
    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

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

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,612
    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 Cprogramming.com
    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, 03: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, 05:05 PM

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