Reputation: 21
I 've encountered this piece of code:
(stuct sockadrr*)&servaddr
What does that mean? You cast the struct somehow to servaddr
?
I'm just trying to figure out the syntax of this code and the meaning of it in C.
Upvotes: 2
Views: 222
Reputation: 16888
Various socket functions are declared to take as a parameter a generic network address, you cast the address of a specific network address struct (e.g. sockaddr_in
) in order to pass it as a parameter to the function.
Upvotes: 2
Reputation: 183908
You cast the struct somehow to servaddr?
No, the other way round, you cast the address of servaddr
to a pointer to struct sockaddr
.
If servaddr
has type foo
, &servaddr
is a foo*
, a pointer to foo
. If you call a function that expects a pointer to struct sockaddr
, you need to cast it - if the types are compatible, so that passing a foo*
as a struct sockaddr*
works.
Upvotes: 3