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

Things get really useful when you combine structs and pointers. The "class" of object oriented programming, as powerful as it is, is to a large extent just a pointer to a struct, a struct-star.

#include /* needed for malloc */
#include  /* 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 Return to Index. #: Continue. #: