What does this mean ?

R

Roka

Hi all
How to explain the *s below.

char **addrs;
...
printf("%s\n",inet_ntoa(*(struct in_addr *)*addrs));


The define of inet_ntoa() is : char *inet_ntoa(struct in_addr in);

Thanks.
 
V

Vladimir S. Oka

Roka said:
Hi all
How to explain the *s below.

char **addrs;
...
printf("%s\n",inet_ntoa(*(struct in_addr *)*addrs));


The define of inet_ntoa() is : char *inet_ntoa(struct in_addr in);

I guess what was tried was:

`addrs` is a pointer to a pointer to `char`
`*addrs` gets you the pointer to `char`
`(struct in_addr *)*addrs` casts that to a pointer to `struct in_addr`
that is then dereferenced to be passed to `inet_ntoa()`

So, it seems that wherever the pointer `addrs` points, is supposed to
contain a series of `char`s, which are supposed to be able to be
interpreted as `struct in_addr`.

Quite abominable, IMHO...

--
BR, Vladimir

Democracy is the theory that the common people know what they want, and
deserve to get it good and hard.
-- H.L. Mencken, "Little Book in C major", 1916
 
D

Dave Thompson

I guess what was tried was:

`addrs` is a pointer to a pointer to `char`
`*addrs` gets you the pointer to `char`
`(struct in_addr *)*addrs` casts that to a pointer to `struct in_addr`
that is then dereferenced to be passed to `inet_ntoa()`

So, it seems that wherever the pointer `addrs` points, is supposed to
contain a series of `char`s, which are supposed to be able to be
interpreted as `struct in_addr`.
Right.

Quite abominable, IMHO...

Poor for standard C, yes. <OT> However, this is the type returned by
the (BSD-to-SUS/POSIX) gethost* functions for the list of network
addresses, since the API was intended to support a whole range of
protocol stacks, of which only 2 survive and only 1 (IP) matters.

The whole BSD API was designed to use a series of structures that are
in fact laid out to be compatible, but are not declared that way to
the C compiler to allow separate development (and extension), and thus
is one of the few places well-written (Unixoid) C _needs_ casts.
In retrospect it is not clear whether this design was really worth it,
but it's far too late to change now. </>

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top