Mail Archives: djgpp/1998/08/06/20:36:10
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 -