/* DEMO2 less trivial use of pointer arithmetic uses a loop to copy then string from name to word. we use 2 pointers to traverse the src and dest strings Note also the use of an empty initialization clause in the for loop */ #include #include #include int main() { char word[10]; char *name = malloc( 10 * sizeof(char) ); char *word_ptr=word, *name_ptr=name; /* the start of each string */ strcpy( name, "timothy" ); /* Copy "timothy" into word */ for ( ; *name_ptr !='\0' ; word_ptr++, name_ptr++ ) *word_ptr = *name_ptr; *word_ptr = '\0'; printf("my name is: %s\n", word ); free( name ); /* return mem to the heap */ return 0; }