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

Depends on what you mean Work? Remember, structs must be defined before any use of them is made.

If the object was to switch the names, this doesn't work. Sure a change was made in the function swapNames, but this change was not permanent.

Because C is call by value, a copy of each structure was made when moving from the procedure mean to the procedure swapNames. The manner in which names were swaped updated the first and last fields in only the copies of the structers, it did not change the original structures in main.

#include

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

void swapNames( struct name n1, struct name n2 ) {
   char temp ;
   while ( *n1.first ) {
      temp = *n1.first ;
      *n1.first = *n2.last ;
      *n2.last = temp ;
      n1.first++ ;
      n2.last++ ;
   }
}

int main(int argc, char * argv) {

  struct name n1 = { "dylan", "thomas" } ;
  struct name n2 = { "bob", "danny" } ;

  swapNames( n1, n2 ) ;

  printf("%s %s\n", n1.first, n1.last ) ;
  printf("%s %s\n", n2.first, n2.last ) ;

}
Will this take care of it? #Shuffle: none $PAGE$-A $PAGE$-B Return to Learn C Introduction #: No, this problem cannot be solved by a computer. #: Yes, the change is made in a string which is referenced by both the original and the copy structure. It will appear changed to both of them. #: