Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在c中使用gethostbyname_C_Sockets_Tcp_Gethostbyname - Fatal编程技术网

如何在c中使用gethostbyname

如何在c中使用gethostbyname,c,sockets,tcp,gethostbyname,C,Sockets,Tcp,Gethostbyname,我试图使用函数gethostbyname,但我的代码: int handleTCP(char *hostname, char* portNo){ struct hostent *hp = gethostbyname(hostname); ... } 不断返回: 21: warning: initialization makes pointer from integer without a cast 有人知道我的语法有什么问题吗 谢谢你忘了#包括。因为您没有包含此文件,所

我试图使用函数gethostbyname,但我的代码:

int handleTCP(char *hostname, char* portNo){

    struct hostent *hp = gethostbyname(hostname);

    ...

}
不断返回:

21: warning: initialization makes pointer from integer without a cast
有人知道我的语法有什么问题吗

谢谢

你忘了
#包括
。因为您没有包含此文件,所以您正在运行“defaultint”规则。基本上,在C中,如果函数没有原型,则假定为:

int函数_name()换句话说“返回一个整数,获取未知数量的参数”


正确声明函数原型(在本例中通过包含头文件)可以避免这种情况。

我认为您忘记了包含
netdb.h
头文件,因此编译器对
gethostbyname
函数一无所知,并假设它返回整数。然后它会抱怨,因为在这种情况下,您会将整数转换为指针。您需要包含一个标题,以便为编译器提供正确的类型信息。

如果Evan的答案不是问题所在:请向我们展示更多代码,尤其是第21行代码是否是您展示给我们的代码之一。您的代码顶部肯定有
#include
。@OliCharlesworth:netdb.h是您所需要的全部。谢谢。这把它修好了。我犯了一个愚蠢的错误。