c - How do I get sin6_addr from an addrinfo? -
my addrinfo pointer looks this-
struct addrinfo hint, *res = null;
i call addrinfo.
hint.ai_family = af_unspec; ret = getaddrinfo(curhost, null, &hint, &res);
curhost character array. doing
saddrv6.sin6_addr=*(res->ai_addr).sin6_addr
is giving me error says
request member 'sin6_addr' in not structure or union. saddrv6 sockaddr_in6 struct. way fill sin6_addr info have in res? new c programming here .
the specific error you're getting because in:
*(res->ai_addr).sin6_addr
the .
operator binds more tightly *
. change to:
(*res->ai_addr).sin6_addr
which meant, better way use ->
operator:
res->ai_addr->sin6_addr
however, still doesn't work because ai_addr
has useless opaque type struct sockaddr *
, not struct sockaddr_in6 *
. fix need cast pointer type points to:
((struct sockaddr_in6 *)res->ai_addr)->sin6_addr
at point code should work. however, ai_addr
member of struct addrinfo
not meant accessed directly rather used abstractly , passed functions connect
, bind
, sendto
, recvfrom
, etc. @ point we're talking matter of style , programming practices rather correctness per language, though.
note if want ipv6 address sake of printing string, getnameinfo
function ni_numerichost
flag lets in abstract way without having poke through opaque struct sockaddr *
.
Comments
Post a Comment