2

我遇到过这段代码:

(stuct sockadrr*)&servaddr 

这意味着什么?您以某种方式将结构转换为servaddr?

我只是想弄清楚这段代码的语法和它在 C 中的含义。

4

2 回答 2

3

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.

于 2012-11-23T16:56:21.923 回答
2

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.

于 2012-11-23T16:57:57.100 回答