www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1998/08/06/20:36:10

Sender: nate AT cartsys DOT com
Message-ID: <35CA4951.D1931D5A@cartsys.com>
Date: Thu, 06 Aug 1998 17:24:49 -0700
From: Nate Eldredge <nate AT cartsys DOT com>
MIME-Version: 1.0
To: Michiel Uitdehaag <m DOT uitdehaag AT imn DOT nl>
CC: djgpp AT delorie DOT com
Subject: Re: libsocket resit demo
References: <35C9D278 DOT E1889095 AT imn DOT nl>

Michiel Uitdehaag wrote:
> 
> Hi,
> 
> I installed the Libsocket TCP library yesterday (from the
> http://www.geocities.com/SiliconValley/Lab/3216/lsck.htm libsocket
> pages) and noticed the resit demo crashing every time.
> So, being the newbie I am and investigating the c code, I found this:
> (resit.c)
> 
> int main (int argc, char *argv[])
> {
>  struct hostent *hpke;
>  char *x;
> 
> ... (snip)
> 
> printf ("Aliases: ");
>   while ( *(hpke->h_aliases ) ) {
>     printf ("%s ", *hpke->h_aliases );    <-----------Here
>     hpke->h_aliases ++;
>   }
> 
> ... <snip rest>
> 
> as hpke is already a pointer, *hpke would dereference it, and force the
> program to use the value at (*hpke) as a pointer to h_aliases, or am I
> gravely mistaking here?
> Shouldn't the correct code be:
> 
> printf ("Aliases: ");
>   while ( *(hpke->h_aliases ) ) {
>     printf ("%s ", hpke->h_aliases );
>     hpke->h_aliases ++;
>   }

Nope.  If the definition of `struct hostent' is the same as on Unix, it
looks like:

struct hostent {
   char    *h_name;        /* official name of host */
   char    **h_aliases;    /* alias list */
   int     h_addrtype;     /* host address type */
   int     h_length;       /* length of address */
   char    **h_addr_list;  /* list of addresses */
};

So the h.aliases member is what is often called a vector; an array of
pointers to strings, terminated by NULL.  A diagram:

h.aliases -->  h.aliases[0] --> "First alias"
               h.aliases[1] --> "Second alias"
               ...
               NULL

And since `->' binds more tightly than `*', that code will indeed walk
through the array of aliases, printing each one.

Thus, the bug is somewhere else.
-- 

Nate Eldredge
nate AT cartsys DOT com


- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019