#Meta-Wlp: #Macro: TITLE wlp: Learn C (5) p. 12 #Eval: $TITLE$ #Macro: PAGE bb5-12

Better syntax

The syntax star-n-dot is sensible but not compelling. The type-stars are generally thought of as pointers to something of that type. You jump through the pointer to get at the data item. A notation that highlights this is n>last. This is precisely (*n).last but said using different characters, because the inventors of C thought it looked more suggestive of the pointer idea.


#include<stdlib.h> /* needed for malloc */
#include<stdio.h>  /* needed for printf */

struct name { 
    char * first ; 
    char * last ; } ;

void nameExchange( struct name * n ) {
    char * t ;
    t = n->first ;
    n->first = n->last ;
    n->last = t ;
}

int main(int argc, char * argv) {

  struct name * n1 ;
  n1 = (struct name *) malloc( sizeof(struct name) ) ;
  n1->first = "dylan" ;
  n1->last = "bob" ;
  nameExchange(n1) ;
  printf("%s %s\n", n1->first, n1->last ) ;

}
#Shuffle: none $PAGE$-A $PAGE$-B Return to index. #: s->f is just a special way of writing (*s).f #: The pointer notation s->f is a startling new thing, for which the operator -> has no equivalent. #: