What does the “s” mean in the structure?

匿名 (未验证) 提交于 2019-12-03 08:33:39

问题:

Here's a simple question. What's the meaning of the leading letter "s" in the sin_family, sin_port, sin_addr and sin_zero?

struct sockaddr_in {     short int          sin_family;  // Address family, AF_INET     unsigned short int sin_port;    // Port number     struct in_addr     sin_addr;    // Internet address     unsigned char      sin_zero[8]; // Same size as struct sockaddr }; 

Thanks.

回答1:

This comes from Berkeley, back when LSD was still legal. So very obvious in their naming choices :/

All kidding aside, this dates back to very early K&R C where structure members didn't have their own namespace. Which required you to come up with distinct names for the structure members that wouldn't collide with identifiers in the global namespace. Painful. Prefixing the names with an abbreviation of the structure name was the common approach.

Thus "sockaddr_in" becomes "sin".

Note how enums still have this problem today, not untypically solved the same way.



回答2:

sin is repeating the name of the sockaddr_in structure, i.e. Socket INternet.



回答3:

"sin" stands for "Socket INternet" in this context.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!