简体   繁体   English

为什么 .net_ntop() 返回一个指针而不是一个 int?

[英]Why does inet_ntop() return a pointer instead of an int?

.net_ntop() has a signature as follows: .net_ntop() 的签名如下:

const char* .net_ntop(int af, const void* src, char* dst, socklen_t size); const char* .net_ntop(int af, const void* src, char* dst, socklen_t size);

Description:描述:

   This function converts the network address structure src in the
   af address family into a character string.  The resulting string
   is copied to the buffer pointed to by dst, which must be a non-
   null pointer.  The caller specifies the number of bytes available
   in this buffer in the argument size.

   On success, inet_ntop() returns a non-null pointer to dst, or NULL
   if there was an error.

The pattern for socket operations (eg, getsockopt(), socket(), bind(), listen(), connect(), etc.) is typically to return an int, indicating (0) success, or (-1) error.套接字操作的模式(例如,getsockopt()、socket()、bind()、listen()、connect() 等)通常返回一个 int,指示 (0) 成功或 (-1) 错误.

It seems redundant for .net_ntop() to return a pointer to the very data structure that the caller passed into it -- dst. .net_ntop() 返回一个指向调用者传递给它的数据结构的指针似乎是多余的——dst。 Obviously, that datum was already known by the caller, since it was required to be passed into the function. There has to be some compelling reason for this to step away from the convention;显然,调用者已经知道该数据,因为它需要传递到 function。必须有一些令人信服的理由才能脱离惯例; returning redundant information surely cannot be such.返回冗余信息肯定不是这样的。

I'm feeling pretty stupid for just not seeing the reason for this.我只是因为没有看到这个原因而感到很愚蠢。 Any insight?有什么见解吗?

While the reason for these choices should be found in the minutes of the committee discussion, or asked to the people who designed the API, I can take a guess.虽然这些选择的原因应该在委员会讨论的记录中找到,或者询问设计 API 的人,但我可以猜测一下。

.net_ntop() is a POSIX function, so it comes from C more than from C++. As you say, the pattern for socket operations is to return an int . .net_ntop()是 POSIX function,因此它来自 C 而不是来自 C++。正如您所说,套接字操作的模式是返回一个int But .net_ntop() is more a string function, so I'd compare it with functions from the string.h library.但是.net_ntop()更像是一个字符串 function,所以我会将它与string.h库中的函数进行比较。 Just consider考虑一下

char *strcpy( char *dest, const char *src );

Why returning the same pointer I passed in?为什么要返回我传入的同一个指针? For chaining operations without declaring multiple temporary variables:对于不声明多个临时变量的链接操作:

char s[] = "this is the origial string I want to copy";
char *my_copy = strcpy(malloc(strlen(s) + 1), s);

(I know that this is bad practice because we are not checking the return value of malloc , but I don't think that the design was taking this into account). (我知道这是不好的做法,因为我们没有检查malloc的返回值,但我认为设计没有考虑到这一点)。

More (really more) guess work is available here .更多(真的更多)猜测工作可在此处获得。

It's useful if you want to reduce the number of code lines.如果您想减少代码行数,这很有用。 You can use the return value directly in printf(), strcpy() etc...您可以直接在 printf()、strcpy() 等中使用返回值...

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

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