IP ADDRESS OF MACHINE

snodx@hotmail.com snodx@hotmail.com
Wed Feb 27 05:13:00 GMT 2002


Greetings,
           Thanks to Stephano Mariani and Robert Lopez for their inputs.

            Robert Lopez wrote:
             >The GNU C Library Reference Manual might be useful to you. See the
sections on Host Addresses and  Host Names in the Sockets section.

            Where can I get the GNU C Library Reference Manual?

            Stephano Mariani wrote:

            >See `man gethostbyname`

            I gather that you suggested that I use this function which exists in the netdb.h header file

            I wrote the following C program:

            #include<unistd.h>
            #include<netdb.h>

            main()
            {
              int snodsize=12;
              char snodarr[100],arrsnod[100],*sndptr,*snodptr,**snodptrptr;
              struct hostent *hstptr;

              sndptr=snodarr;
              snodptr=arrsnod;
              snodptrptr=&snodptr;

                gethostname(sndptr,(size_t)snodsize);

              hstptr=gethostbyname(sndptr);
              snodptrptr=hstptr->h_addr_list;

                printf("%s",*snodptrptr);
            }
___________________________________________________________________________________________________

        The program is simple to understand:

        It declares a pointer to a character ARRAY called sndptr

        It declares another pointer to another character array snodptr

        It declares a pointer to a character pointer called snodptrptr

        It retrieves the hostname of the current system using gethostname of unistd.h

        It retrieves a pointer to a hostent structure using gethostbyname of netdb.h

        Next I equate the pointer-to-character-pointer variable snodptrptr
        to the h_addr_list field of the hostent structure. This is because
        h_addr_list is the most likely field where the IP Address might be
        stored and it is a pointer-to-pointer which cannot be accessed directly.
        Hence snodptrptr

        I display snodptrptr.

        But I am not getting the IP Address as I wanted it.
__________________________________________________________________________________________________

         What am I doing wrong? Are there any modifications in the code?
         SNODX



More information about the Gcc-help mailing list