Wednesday, November 2, 2011

strcpy( ) in C programming language

This function copies the contents of one string into another. The base addresses of the source and target strings should be supplied to this function. Here is an example of strcpy( ) in action...

main( )
{
      char  source[ ] = "Sayonara" ;
      char  target[20] ;

      strcpy ( target, source ) ;
      printf ( "\nsource string = %s", source ) ;
      printf ( "\ntarget string = %s", target ) ;
}

And here is the output...

source string = Sayonara
target string = Sayonara

On supplying the base addresses, strcpy( ) goes on copying the characters in source string into the target string till it doesn't encounter the end of source string (‘\0’). It is our responsibility to see to it that the target string’s dimension is big enough to hold the string being copied into it. Thus, a string gets copied into another, piece-meal, character by character. There is no short cut for this. Let us now attempt to mimic strcpy( ), via our own string copy function, which we will call xstrcpy( ).

main( )
{
      char  source[ ] = "Sayonara" ;
      char  target[20] ;

      xstrcpy ( target, source ) ;
      printf ( "\nsource string = %s", source ) ;
      printf ( "\ntarget string = %s", target ) ;
}

xstrcpy ( char  *t, char  *s )
{
      while ( *s != '\0' )
 {
            *t = *s ;
  s++;
  t++ ;
 }
      *t = '\0' ;
}

The output of the program would be...

source string = Sayonara
target string = Sayonara

Note that having copied the entire source string into the target string, it is necessary to place a ‘\0’ into the target string, to mark its end. If you look at the prototype of strcpy( ) standard library function, it looks like this…

strcpy ( char *t, const char *s ) ;

We didn’t use the keyword const in our version of xstrcpy( ) and still our function worked correctly. So what is the need of the const qualifier? What would happen if we add the following lines beyond the last statement of xstrcpy( )?.

s = s - 8 ;
*s = 'K' ;

This would change the source string to “Kayonara”. Can we not ensure that the source string doesn’t change even accidentally in xstrcpy( )? We can, by changing the definition as follows:

void xstrcpy ( char *t, const char *s )
{
      while ( *s != '\0' )
 {
*t = *s ;
  s++ ;
  t++ ;
 }
      *t = '\0' ;
}

By declaring char *s as const we are declaring that the source string should remain constant (should not change). Thus the const qualifier ensures that your program does not inadvertently alter a variable that you intended to be a constant. It also reminds anybody reading the program listing that the variable is not intended to change. We can use const in several situations. The following code fragment would help you to fix your ideas about const further.

char *p = "Hello" ;  /* pointer is variable, so is string */
*p = 'M' ;  /* works */
p = "Bye" ;  /* works */

const char *q = "Hello" ;  /* string is fixed pointer is not */
*q = 'M' ;  /* error */
q = "Bye" ;  /* works */

char const *s = "Hello" ;  /* string is fixed pointer is not */
*s = 'M' ;  /* error */
s = "Bye" ;  /* works */

char * const t = "Hello" ;  /* pointer is fixed string is not */
*t = 'M' ;  /* works */   
t = "Bye" ;  /* error */

const char * const u = "Hello" ;  /* string is fixed so is pointer */
*u = 'M' ;  /* error */
u = "Bye" ;  /* error */

The keyword const can be used in context of ordinary variables like int, float, etc. The following program shows how this can be done.

main( )
{
      float r, a ;
      const float pi = 3.14 ;

      printf ( "\nEnter radius of circle " ) ;
      scanf  ( "%f", &r ) ;
      a = pi * r * r ;
      printf ( "\nArea of circle = %f", a ) ;
}

No comments:

Post a Comment