我通常会看到表单的结构
struct Employee {
int age;
char* name;
}我查看了微软Winsock 2的“入门”,并看到了以下内容:
struct addrinfo *result = NULL,
*ptr = NULL,
hints;
ZeroMemory( &hints, sizeof(hints) );
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_protocol = IPPROTO_TCP;这是什么结构?我计算出结构的名称是addrinfo,但是*result、*ptr或hints是哪种类型?
另外,当hints以前从未被编码时,它是如何被赋予.ai_family/socktype/protocol的呢?
发布于 2015-09-16 20:16:58
struct addrinfo是一种包含网络地址信息的标准数据结构。它由例如POSIX,又名SingleUnix定义。你可以通过包括netdb.h或者你的操作系统的等价物来获得它。它的字段(至少)是:
int ai_flags Input flags.
int ai_family Address family of socket.
int ai_socktype Socket type.
int ai_protocol Protocol of socket.
socklen_t ai_addrlen Length of socket address.
struct sockaddr *ai_addr Socket address of socket.
char *ai_canonname Canonical name of service location.
struct addrinfo *ai_next Pointer to next in list. struct addrinfo *result = NULL, *ptr = NULL, hints;
result和ptr是指向struct addrinfo的指针。hints是一个堆栈分配的struct addrinfo.
Windows是一个纯C,因此它使用C约定。必须用struct明确地声明C中的结构变量。不能像在struct中那样关闭C++。它们也没有初始化,因此memset()^WZeroMemory()
发布于 2015-09-16 20:14:16
这是一种C风格的方法,可以将变量声明为结构的实例。addrinfo是在其他地方定义的结构,result是指向一个实例的指针。以下是addrinfo的实际定义
在现代C++中,以下是等价的:
addrinfo* result = NULL; // nullptr in C++11 and beyond
addrinfo* ptr = NULL; // nullptr in C++11 and beyond
addrinfo hints;https://stackoverflow.com/questions/32617779
复制相似问题