Is it possible to cast struct to another?

后端 未结 4 557
名媛妹妹
名媛妹妹 2020-12-28 19:03

Any one could describe how (struct sockaddr *)&server works here? Is it possible to cast bigger struct to smaller struct?

See these structs:

4条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-28 19:36

    This is C's form of "inheritance" (notice the quotes). This works because C does not care about the underlying data in an address, just what you represent it as.

    The function determines what structure it actually is by using the sa_family field, and casting it into the proper sockaddr_in inside the function.

提交回复
热议问题