C Library Functions -strcpy()

C Standard Library - <string.h>

Description

C Library Functionschar *strcpy(char *dest, const char *src)holdsrccopies the string pointed to todest。

It should be noted that if the destination array dest is not large enough and the source string is too long, it may cause buffer overflow.

Declaration

Below is the declaration of the strcpy() function.

char *strcpy(char *dest, const char *src)

Parameter

  • dest-- Points to the destination array used to store the copied content.
  • src-- the string to be copied.

Return Value

This function returns a pointer to the final destination string dest.

Example

The following example demonstrates the usage of the strcpy() function.

Example 1

#include <stdio.h> #include <string.h> int main() { char src[40]; char dest[100]; memset(dest, '\0', sizeof(dest)); strcpy(src, "This is example.com"); strcpy(dest, src); printf("The final target string: %s\n", dest); return(0); }

Let us compile and run the above program, which will produce the following result:

最终的目标字符串: This is example.com

Example 2

#include <stdio.h> #include <string.h> int main () { char str1[]="Sample string"; char str2[40]; char str3[40]; strcpy (str2,str1); strcpy (str3,"copy successful"); printf ("str1: %s\nstr2: %s\nstr3: %s\n",str1,str2,str3); return 0; }

Let us compile and run the above program, which will produce the following result:

str1: Sample string
str2: Sample string
str3: copy successful

C Standard Library - <string.h>

other extensions