简体   繁体   English

Winsock 2 addrinfo *结果?

[英]Winsock 2 addrinfo *result?

I'm learning C++ through online tutorials such as the one at cprogramming.com , and have decided to create a simple socket program as my first real project. 我正在通过在线教程(例如cprogramming.com上的在线教程)学习C ++ ,并决定创建一个简单的套接字程序作为我的第一个实际项目。 I have already experimented with basic functions and get the gist of how C++ works. 我已经尝试过基本功能,并了解C ++的工作原理。 I just ran into something on the MSDN Winsock2 walk-through that confused me. 我只是在MSDN Winsock2演练中遇到了让我感到困惑的事情。

On this page , an object named hints is declared from the sddrinfo structure: 此页面上 ,从sddrinfo结构中声明了一个名为hints的对象:

struct addrinfo *result = NULL,
                *ptr = NULL,
                hints;

I am confused about the *result and *ptr = null parts of this declaration. 我对该声明的* result和* ptr = null部分感到困惑。 Since there are no semicolons i assume the newlines are for readability purposes and this code can be written like this. 由于没有分号,因此我认为换行符是出于可读性目的,因此可以这样编写代码。

struct addrinfo *result = NULL, *ptr = NULL, hints;

It appears that we are declaring two pointers that point to the addrinfo struct and setting them to null along with declaring a hints object. 看来我们正在声明两个指向addrinfo结构的指针,并将它们设置为null以及声明一个hints对象。 Can someone explain the purpose of setting these to NULL ? 有人可以解释将这些设置为NULL的目的吗? And if anyone is familiar with winsock can you explain how and why these pointers are used? 如果有人熟悉Winsock,您可以解释如何以及为什么使用这些指针吗? Why not just use the hints object? 为什么不只使用提示对象?

That line declares multiple variables in one line. 该行在一行中声明了多个变量。 It's the same thing as this 这是一样的

struct addrinfo *result = NULL;
struct addrinfo *ptr = NULL;
struct addrinfo hints;

Programmers set pointers to NULL so that if you accidentally dereference them, you'll throw an exception and find your error. 程序员将指针设置为NULL,这样,如果您不小心取消引用了它们,就会抛出异常并查找错误。 If you don't initialize them, they'll point to junk which can cause your application to crash in devious manners. 如果不初始化它们,它们将指向垃圾,这可能导致您的应用程序以vious回的方式崩溃。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM