我遇到过这段代码:
(stuct sockadrr*)&servaddr
这意味着什么?您以某种方式将结构转换为servaddr
?
我只是想弄清楚这段代码的语法和它在 C 中的含义。
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.
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.