gcc - Help understanding -> operator in C -
possible duplicate:
arrow operator (->) usage in c
i new c, , trying understand function (see below).
the main parts confusing me are:
sp->s_port = htons(sp->s_port);
and
p = sp->s_port;
i'm not sure understand -> operator.
here whole function... hostbufferlength set 1024 (not sure if matters)
int gi_get_port (char *serv, char *prot) /* obtain port named service */ { int p, s; /* data resolving service name socket description. */ struct servent *sp = null; char gsbn_servbuf[hostbufferlength] = {0}; struct servent gsbn_sp; struct servent *gsbn_serv_result; int gsbn_s = 0; gsbn_s = getservbyname_r(serv, prot, &gsbn_sp, gsbn_servbuf, sizeof(gsbn_servbuf), &gsbn_serv_result); sp = gsbn_serv_result; sp->s_port = htons(sp->s_port); if (sp && socket_debug) { printf("%s get_service_by_name - service: %s port: %d protocol: %s\n", get_timestamp(), sp->s_name, sp->s_port, sp->s_proto); } if (sp == null) { fprintf(stderr, "%s get_service_by_name - service %s not found.\n", get_timestamp(), serv); } if (sp != null) { p = sp->s_port; } else { p = -1; }; return p; }
the ->
operator shorthand dereferencing pointer , accessing member of structure points to.
foo->x
can done in place of
(*foo).x
Comments
Post a Comment