Segmentation Fault

Posted by Biranchi on Stack Overflow See other posts from Stack Overflow or by Biranchi
Published on 2010-05-25T17:18:50Z Indexed on 2010/05/25 17:21 UTC
Read the original article Hit count: 179

Filed under:
|
|
|

Hi All,

I have the following piece of code for getting the hostname and IP address,

#include <stdlib.h>
#include <stdio.h>
#include <netdb.h> /* This is the header file needed for gethostbyname() */
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>


int main(int argc, char *argv[])
{
struct hostent *he;

if (argc!=2){
printf("Usage: %s <hostname>\n",argv[0]);
exit(-1);
}

if ((he=gethostbyname(argv[1]))==NULL){
printf("gethostbyname() error\n");
exit(-1);
}

printf("Hostname : %s\n",he->h_name); /* prints the hostname */
printf("IP Address: %s\n",inet_ntoa(*((struct in_addr *)he->h_addr))); /* prints IP address */
}

but i am getting a warning and segmentation fault as

host.c: In function ‘main’: host.c:24: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’

What is the error in the code ??

Thanks

© Stack Overflow or respective owner

Related posts about c

    Related posts about hostent