简体   繁体   English

为什么我们在尝试使用 c 中的 sockets 连接到服务器时使用 * 和 &

[英]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.

相关问题 TCP-IP套接字C:尝试连接到服务器套接字时出现错误的地址错误 - TCP-IP sockets C: Bad address error when trying to connect to server socket 当用C中的指针定义时,为什么我们使用`const`作为字符串? - Why do we use `const` for strings when defining with pointers in C? 当我们在 c 中有更简单的代码时,为什么要使用 malloc 或 calloc? - Why do we use malloc or calloc when we have a simpler code in c? c套接字 - 无法连接到服务器 - c sockets-cant connect to the server 限制C标准I / O以及为什么我们不能将C标准I / O与套接字一起使用 - Restriction of C standard I/O and why we can't use C standard I/O with sockets 为什么在尝试使用指针访问数组元素时不使用间接运算符? - Why do we not use an indirection operator when trying to access element of an array using a pointer? 为什么我们在 C 中使用 \r 和 \b? - why do we use \r and \b in C? 我们什么时候使用goto * expr; 在C? - When do we use goto *expr; in C? 为什么在C中读取文件时,我们必须使用字符数组? - Why in C, when reading files, do we have to use a character array? C/C++:我们何时以及为什么需要在 TLS 客户端-服务器应用程序中调用 SSL_do_handshake()? - C/C++: When and why do we need to call SSL_do_handshake() in a TLS client-server application?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM