开发者

Help understanding -> operator in C [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Arrow operator (->) usage in C

I am new to C, and I am trying to understand this function (see below).

The main parts that are confusing me are:

sp->s_port = htons(SP->s_port); 

and

p = sp->s_port;

I'm not sure I understand the -> operator.

Here is the whole function... HOSTBUFFERLENGTH is set to 1024 (not sure if that matt开发者_JAVA技巧ers)

int gi_get_port (char *serv, char *prot)
/* obtain the port for the named service */
{
  int p, s;

  /* Data for resolving service name to a 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 is shorthand for dereferencing a pointer and then accessing a member of the structure that it points to.

foo->x

can be done in place of

(*foo).x


p->y is shorthand for (*p).y


There are at least three ways in C to get a specific field given a pointer to a struct:

p->y
(*p).y
p[0].y

In the big picture, the need for the -> operator stems from the fact that C's dereference operator is prefix rather than postfix, so it's too hard to use for the common case of structure dereference.

The Pascal language did have one nice feature: ^ which was a postfix dereference operator. If C had the same thing you could write:

p^.y // or perhaps...
p*.y

DMR appeared to state1 in one paper that he would have switched to postfix dereference early on but C was already too well established. The expression syntax is reasonably workable either way but it would also have untangled the C declaration syntax and removed most of the need to read them in an inside-out way that people consistently have trouble with:

int *fp();
int fp()*;    // alternate universe

int (*pf)();
int pf*();    // alternate universe

int *(*pfp)();
int pfp*()*;  // alternate universe

1. See The Development of the C Language., Dennis M. Ritchie

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜