strcpy

String copy or strcpy is a library function. We are discussing how this function works. It takes two buffers- first one the the destination buffer where string should be copied. This can be an uninitialized buffer. Source buffer is the second argument. This should be a NULL terminated string. Strcpy has no way to validate destination buffer length thus programmer should provide a buffer which is at least the size of source string (destination buffer size >= source string). If destination buffer is smaller than source then strcpy can corrupt data or heap segment depending on the location of destination buffer.

strcpy - pictorial view

strcpy - pictorial view

strcpy - source code

/*************************************************** * FUNCTION: strcpy() * __________________________________________________ * * DESCRIPTION: * Copies source string to destination string. * * INPUTS: * * dest_ptr - Destination pointer of this operation * src_ptr - Source string of this operation. * * OUTPUTS: * None. * * RETURNS: * Returns the destination string. * * NOTES: * ***************************************************/
char* strcpy(char * dest_ptr, const char * src_ptr)
{
  char* strresult = dest_ptr;
  if((NULL != dest_ptr) && (NULL != src_ptr))
  {
    /* Start copy src to dest */
    while (NULL != *src_ptr)
10      {
11        *dest_ptr++ = *src_ptr++;
12      }
13      /* put NULL termination */
14      *dest_ptr = NULL;
15    }
16    return strresult;
17 
18  }/* End: strcpy() */

strcat

String Concatenation or strcat is again a library function. It also take two buffer, first one the the destination buffer where string should be combined. This should be a null terminated string. Source buffer is the second argument. This should also be a NULL terminated string. Strcat has no way to validate destination buffer length thus programmer should provide a buffer which is at least the combined size of source string and destination string (destination buffer size >= source string + destination string). If destination buffer is smaller than it can corrupt data or heap segment depending on the location of destination buffer.

stacat - pictorial view

strcat - pictorial view

strcat - source code

/*************************************************** * FUNCTION: strcat() * __________________________________________________ * * DESCRIPTION: * Concatenates source string to destination. * * INPUTS: * dest_ptr - Destination pointer of this operation * value - Source string for this operation * * OUTPUTS: * None. * * RETURNS: * Returns the destination string. * * NOTES: * ***************************************************/
char* strcat(char* dest_ptr,const char * src_ptr)
{
  char* strret = dest_ptr;
  if((NULL != dest_ptr) && (NULL != src_ptr))
  {
    /* Iterate till end of dest string */
    while(NULL != *dest_ptr)
10      {
11        dest_ptr++;
12      }
13      /* Copy src string starting from the end NULL of dest */
14      while(NULL != *src_ptr)
15      {
16        *dest_ptr++ = *src_ptr++;
17      }
18      /* put NULL termination */
19      *dest_ptr = NULL;
20    }
21    return strret;
22 
23  }/* End : strcat() */

Note: Length of a buffer is calculated by (string length + 1). One extra character is for taking NULL termination into consideration. While allocating memory correct allocation should be malloc(strlen(src)+1);.

You have viewed 1 page out of 248. Your C learning is 0.00% complete. Login to check your learning progress.

 Vote 0

Similar topics related to this section

variadic functions, variadic macro, multiple arguments in printf and scanf, strcpy and strcat source, memcpy vs memmove, strrev source, strdup, strtok,

# C Programming Language (Prentice Hall Software)
# Let Us C Paperback - 2006 by Yashavant Kanetkar
# Understanding and Using C Pointers Core techniques for memory management
# Data Structures Using C and C++ Paperback - 1998
# Data Structures In C Paperback - August 11, 2008 by Noel Kalicharan