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);?
It should be called like this:
As for why, this is the C way of doing polymorphism, if you look at the definitions of those structures:
They all share the same first member,
sa_familywhen you pass a pointer to one of those structures tobind()you first cast it tostruct sockaddr *and inside the function,sa_familyis 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 acceptssockaddr*.Another way to look at it, from an OOP perspective, imagine that
sockaddris the base class forsockaddr_inandsockaddr_in6, and passing a pointer tosockaddris similar to casting to the base type and calling a generic function. hope this makes it more clear.