Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8811877
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T03:28:13+00:00 2026-06-14T03:28:13+00:00

I am confused by this argument to the function which is defined as int

  • 0

I am confused by this argument to the function which is defined as

int bind(int s, const struct sockaddr *name, int namelen)

and called as

bind(sd, (struct sockaddr_in *) &addr, length);

I’m unable to interpret what struct sockaddr_in * means here.

Would this work: bind (sd, &addr, length);?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T03:28:15+00:00Added an answer on June 14, 2026 at 3:28 am

    It should be called like this:

    bind (sd, (struct sockaddr *) &addr, length);
    

    As for why, this is the C way of doing polymorphism, if you look at the definitions of those structures:

    struct sockaddr {
        unsigned short    sa_family;    // address family, AF_xxx
        char              sa_data[14];  // 14 bytes of protocol address
    };
    
    // IPv4 AF_INET sockets:
    struct sockaddr_in {
        short            sin_family;   // e.g. AF_INET, AF_INET6
        unsigned short   sin_port;     // e.g. htons(3490)
        struct in_addr   sin_addr;     // see struct in_addr, below
        char             sin_zero[8];  // zero this if you want to
    };
    
    // IPv6 AF_INET6 sockets:
    struct sockaddr_in6 {
        u_int16_t       sin6_family;   // address family, AF_INET6
        u_int16_t       sin6_port;     // port number, Network Byte Order
        u_int32_t       sin6_flowinfo; // IPv6 flow information
        struct in6_addr sin6_addr;     // IPv6 address
        u_int32_t       sin6_scope_id; // Scope ID
    };
    

    They all share the same first member, sa_family when you pass a pointer to one of those structures to bind() you first cast it to struct sockaddr * and inside the function, sa_family is used to determine which structure you passed and cast back to the right one, instead of having one function for each structure you have one function that accepts sockaddr*.

    Another way to look at it, from an OOP perspective, imagine that sockaddr is the base class for sockaddr_in and sockaddr_in6, and passing a pointer to sockaddr is similar to casting to the base type and calling a generic function. hope this makes it more clear.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am so confused with this code in the book : typedef int (*healthCalcFunc)
just a bit confused by this code var counter = function() { //... var
Totally confused by this one... We have a WAMPServer installation set up, running a
I'm pretty confused by this one and can't think of anything obvious that I
I'm confused about this scope behavior: class Bar: def __init__(self): for fn in [open,openW,remove,mkdir,exists,isdir,listdir]:
I've been confused by this problem for a very long time. I start the
I'm confused about this. Most of us have been told that there isn't any
I'm totally confused on this matter, some say use tables and others say it's
I am confused with this thing, is emulator safe from pc network's firewall like
I’m confused about this; instead of writing tweepy codes like- auth=tweepy.OAuthHandler(consumer_key, consumer_secret) auth.set_access_token(access_token, access_token_secret)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.