通过函数参数列表返回结果? [英] Returning results through the function parameter list?

查看:95
本文介绍了通过函数参数列表返回结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


请考虑以下代码:


/ * resolve_hostname

这会将主机名解析为一个ip地址。

* /

static void resolve_hostname(char result [MAXSIZE],const char

hostname [MAXSIZE],const char server [MAXSIZE])

{

struct hostent * hp_ptr; / *查询结构的指针* /

struct hostent hp; / *内存中的静态结构保存

查询结果* /

unsigned char * chptr; / *地址列表的字符指针* /

struct in_addr in; / * inet_ntoa转换结构* /

unsigned int temp;


/ *这是为了使编译器在未使用的变量上静音

警告。 * /

strcpy(结果,服务器);


/ *网络电话:使用

DNS转发解析主机名到IP地址查询* /

hp_ptr = gethostbyname2(主机名,AF_INET);


/ *处理结果* /

hp = * hp_ptr ;

chptr =(unsigned char *)* hp.h_addr_list;

temp = * chptr;

chptr ++;

temp = temp + * chptr * 256;

chptr ++;

temp = temp + * chptr * 65536;

chptr ++;

temp = temp + * chptr * 16777216;

in.s_addr = temp;

result = inet_ntoa(in);

if(!check_ip_addr(result))showerror(1,verbose);

}


问题是结果不是通过参数返回的

结果。现在,如果我在结束括号之前把这个语句,fprintf(stdout,"%s \ n",result);,

,我打印出结果,但它不会

返回调用例程。现在我之前已经做了类似的事情

,我确实检查了我的旧代码,但我没看到问题是什么。

是。


有什么想法吗?


-

Daniel Rudy


电子邮件地址已被提供编码以减少垃圾邮件。

删除所有号码,然后删除无效,电子邮件,否和垃圾邮件进行回复。

Hello,

Consider the following code:

/* resolve_hostname
this resolves the hostname into an ip address.
*/
static void resolve_hostname(char result[MAXSIZE], const char
hostname[MAXSIZE], const char server[MAXSIZE])
{
struct hostent *hp_ptr; /* pointer for query structure */
struct hostent hp; /* static structure in memory to hold
query results */
unsigned char *chptr; /* character pointer for address list */
struct in_addr in; /* inet_ntoa conversion structure */
unsigned int temp;

/* this is to silence the compiler on a unused variable
warning. */
strcpy(result, server);

/* network call: resolve hostname to ip address using
DNS forward lookup */
hp_ptr = gethostbyname2(hostname, AF_INET);

/* process results */
hp = *hp_ptr;
chptr = (unsigned char *)*hp.h_addr_list;
temp = *chptr;
chptr++;
temp = temp + *chptr * 256;
chptr++;
temp = temp + *chptr * 65536;
chptr++;
temp = temp + *chptr * 16777216;
in.s_addr = temp;
result = inet_ntoa(in);
if (!check_ip_addr(result)) showerror(1, verbose);
}

The problem is that the result is not returned through the parameter
result. Now if I put this statement, fprintf(stdout, "%s\n", result);,
before the closing brace, I get the result printed out, but it doesn''t
return to the calling routine. Now I have done something like this
before, and I did check my old code, but I don''t see what the problem
is.

Any ideas?

--
Daniel Rudy

Email address has been encoded to reduce spam.
Remove all numbers, then remove invalid, email, no, and spam to reply.

推荐答案

您应该使用:

strcpy(结果,inet_ntoa(in),MAXSIZE);

而不是:

result = inet_ntoa(in) ;


原因在inet_ntoa的手册中,请注意最后的

句子:


inet_ntoa ()函数将网络字节顺序中给定

的Internet主机地址转换为标准数字和字符串中的字符串nota

tion。该字符串在静态分配的缓冲区中返回,后续调用将覆盖


You should use:
strcpy(result, inet_ntoa(in), MAXSIZE);
instead of:
result = inet_ntoa(in);

The reason is in the manual of inet_ntoa, please notice the last
sentence:

The inet_ntoa() function converts the Internet host address in given
in network byte order to a string in standard numbers-and-dots nota-
tion. The string is returned in a statically allocated buffer, which
subsequent calls will overwrite.


大约在5 /的时间10/2005 6:11 PM, zy*****@gmail.com 声明了

以下:
At about the time of 5/10/2005 6:11 PM, zy*****@gmail.com stated the
following:
你应该使用:
strcpy(结果,inet_ntoa(in),MAXSIZE);
而不是:
result = inet_ntoa( in);

原因在于inet_ntoa的手册,请注意最后一句话:

inet_ntoa()函数转换给定的Internet主机地址
网络字节顺序为标准数字和字符串的字符串。该字符串在静态分配的缓冲区中返回,后续调用将覆盖。
You should use:
strcpy(result, inet_ntoa(in), MAXSIZE);
instead of:
result = inet_ntoa(in);

The reason is in the manual of inet_ntoa, please notice the last
sentence:

The inet_ntoa() function converts the Internet host address in given
in network byte order to a string in standard numbers-and-dots nota-
tion. The string is returned in a statically allocated buffer, which
subsequent calls will overwrite.




非常感谢您指出这一点。它现在有效。但有一点,

如果指定大小

参数,它应该是strncpy,而不是strcpy。它不会用strcpy编译。


现在我不明白为什么在做result = inet_ntoa(in);

结果时在函数退出时可以使用

strncpy(结果,inet_ntoa(in),MAXSIZE);导致值被写入

回到调用者。

是因为结果的实际指针值指向那个

缓冲区和当函数退出引用时会丢失因为它们是两个不同的缓冲区吗?只是一个想法。


-

Daniel Rudy


电子邮件地址已被编码以减少垃圾邮件。

删除所有号码,然后删除无效,电子邮件,号码和垃圾邮件以进行回复。



Well thank you for pointing that out. It works now. One thing though,
it should probably be strncpy, not strcpy if you specify the size
parameter. It wouldn''t compile using strcpy.

Now what I don''t understand is why when doing result = inet_ntoa(in);
the result is available until the function exits while doing
strncpy(result, inet_ntoa(in), MAXSIZE); causes the value to be written
back to the caller.
Is it because the actual pointer value of result is pointing to that
buffer and when the function exits the reference is lost because they
are two different buffers? Just a thought.

--
Daniel Rudy

Email address has been encoded to reduce spam.
Remove all numbers, then remove invalid, email, no, and spam to reply.


Daniel Rudy< no **** @ nospam达网络>写道:
Daniel Rudy <no****@nospam.net> writes:
大约在5/10/2005 6:11 PM时, zy ***** @ gmail.com 声明
以下内容:
At about the time of 5/10/2005 6:11 PM, zy*****@gmail.com stated the
following:
你应该使用:
strcpy(结果,inet_ntoa(in),MAXSIZE);
而不是:
result = inet_ntoa(in);
You should use:
strcpy(result, inet_ntoa(in), MAXSIZE);
instead of:
result = inet_ntoa(in);


[...]
非常感谢您指出这一点。它现在有效。但有一件事,
如果指定大小
参数,它应该是strncpy,而不是strcpy。它不会使用strcpy进行编译。

[...]
Well thank you for pointing that out. It works now. One thing though,
it should probably be strncpy, not strcpy if you specify the size
parameter. It wouldn''t compile using strcpy.



[...]


小心使用strncpy()。在某些情况下,它不会使用''\0''终止

目标字符串。在其他情况下,它浪费地写了

多个''\ 0''字符。

之后你可能最好先使用strcpy(),确保它不会溢出。


-

Keith Thompson(The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。


[...]

Be careful with strncpy(). In some cases, it doesn''t terminate the
target string with a ''\0''. In other cases, it wastefully writes
multiple ''\0'' characters. You may be better off using strcpy() after
first ensuring that it won''t overflow.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


这篇关于通过函数参数列表返回结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆