|
下面是我照着书上敲的一个程序,运行时,传递一个保存机器名的字符串(如"www.sohu.com")给gethostbyname(),然后从返的数据结构struct hostent中收集信息,并输出.
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <sys/types.h>
#include <netinet/in.h>
int main(int argc,char* argv[]){
struct hostent *h;
char** aliases,addes;
char str[30];
if(argc!=2){
printf("incorrect parameter number\n");
exit(1);
}
if((h=gethostbyname(argv[1]))==NULL){
printf("incorrect exit");
exit(1);
}
printf("Host name:%s\n",h->h_name);
for(aliases=h->h_aliases;*aliases!=NULL;aliases++)
printf("Aliase name:%s\n",*aliases);
addes=h->h_addr_list;
for(;*addes!=NULL;addes++)
printf("IP Address:%s\n",inet_ntoa(*((struct in_addr*) (*addes))));
exit(0);
}
编译时如下:
[root@localhost ~]# gcc -o test test.c
test.c: 在函数 ‘main’ 中:
test.c:24: 警告:赋值时将指针赋给整数,未作类型转换
test.c:25: 错误:invalid type argument of ‘unary *’
test.c:26: 错误:invalid type argument of ‘unary *’
请问是什么原因啊?多谢了 |
|