[英]why do we use * and & when trying to connect to a server with sockets in c
connect(socket_desc , (struct sock_address *)&server , sizeof(server)
why do we use * and & when trying to connect to a server or bind a server its like making a pointer or something but i believe that * doesn't make a pointer when combined with so why putting them both?为什么我们在尝试连接到服务器或绑定服务器时使用 * 和 & 就像制作指针或其他东西一样,但我相信 * 与它们结合时不会产生指针,那么为什么将它们都放在一起呢? thanks for any answers.感谢您的任何回答。
(struct sock_address *)&server
simply states: use the address of server
, cast to a "pointer to sock_address structure".简单地说:使用server
的地址,转换为“指向 sock_address 结构的指针”。
You need to give the address because the function wants to change the things in that structure.您需要提供地址,因为 function 想要更改该结构中的内容。 And the cast is simply so it's the correct type.而且演员阵容很简单,所以它是正确的类型。
It's not the same as:它与以下内容不同:
int num = 42;
int num2 = *(&num); // num would be better.
which would be the two cancelling each other out.这将是两个相互抵消。
While *ptr
dereferences ptr
to give you the underlying value, (type *)ptr
instead treats ptr
as if it was a pointer to type
- there is no dereferencing in the latter case.虽然*ptr
取消引用ptr
为您提供基础值,但(type *)ptr
反而将ptr
视为指向type
的指针 - 在后一种情况下没有取消引用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.