Information About

Strcpy




In Computing , the C Programming Language offers a Library Function called strcpy that allows null-terminated memory blocks to be copied to different locations. Since, in C, strings are not first-class Datatype s, and are implemented as blocks of ASCII Byte s in memory, strcpy will effectively copy strings given two Pointer s to blocks of allocated memory.

For example
  • str1 = malloc(sizeof(char)---LARGE_NUMBER);

  • str2 = malloc(sizeof(char)---LARGE_NUMBER);


fgets(str1, LARGE_NUMBER, stdin);
  • the argument order makes it like an assignment - str2 "=" str1 ---/


Although the simple assignment str2 = str1 might appear to do the same thing, it only copies the memory address of str1 into str2, so the variables now point at the same region of memory (thus any modification made to one string will affect the other). This is known as a Shallow Copy because it does not actually create a new, identical string.

The strcpy function performs a Deep Copy by iterating over the individual characters of the string and copying them one by one. Here are two possible implementations of strcpy:

  • strcpy(char--- dest, const char--- src)

  • {

  • (dest++) = ---(src++) );

  • return dest;

}


  • dest, const char ---src)

  • {

char c;
  • p;

  • q;



EXTERNAL LINKS