Copying a string using array notation and pointer notation : String Copy « String « C Tutorial






#include <stdio.h>

void copy1( char *s1, const char *s2 ); 
void copy2( char *s1, const char *s2 ); 

int main()
{
   char string1[ 10 ];          
   char *string2 = "Hello";     
   char string3[ 10 ];          
   char string4[] = "Good Bye"; 

   copy1( string1, string2 );
   printf( "string1 = %s\n", string1 );

   copy2( string3, string4 );
   printf( "string3 = %s\n", string3 );

   return 0;

}

void copy1( char *s1, const char *s2 )
{
   int i;

   for ( i = 0; ( s1[ i ] = s2[ i ] ) != '\0'; i++ ) {
      ;   
   } 
}

void copy2( char *s1, const char *s2 ) {
   for ( ; ( *s1 = *s2 ) != '\0'; s1++, s2++ ) {
      ;   
   } 
}
string1 = Hello
string3 = Good Bye








3.8.String Copy
3.8.1.Copying a string using array notation and pointer notation
3.8.2.Take a first name and a last name and combines the two strings