C Library Function - strcpy()
Description
C Library Function **char *strcpy(char dest, const char src) Puts the src copies the string pointed to by dest.
Note that if the destination array dest is not large enough and the source string is too long, a buffer overflow may occur.
Declaration
Below is the declaration of the strcpy() function.
char *strcpy(char *dest, const char *src)
Parameters
- dest -- points to the destination array used to store the copied content.
- src -- the string to copy.
Return Value
This function returns a pointer to the final destination string dest.
Example
The following example demonstrates the use 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 runoob.com
"
)
;
strcpy
(
dest
,
src
)
;
printf
(
"
Final destination string: %s
\
n
"
,
dest
)
;
return
(
0
)
;
}
Let's compile and run the above program, which will produce the following result:
最终的目标字符串: This is runoob.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's compile and run the above program, which will produce the following result:
str1: Sample string
str2: Sample string
str3: copy successful
C Standard Library -