[C Language] Implementation of strcpy function, strcpy Function
Basic Content of the strcpy function:
Prototype Declaration: extern char * strcpy (char * dest, const char * src );
Header file: # include <string. h> and # include <stdio. h> function: copy the string starting from the src address and containing the NULL terminator to the address space starting with dest. Description: the memory areas specified by src and dest cannot overlap and dest must have enough space to hold src strings. Returns the pointer to dest. Now that you know the function content, we can simply implement this function:
# Include <stdio. h> # include <assert. h> char * my_strcpy (char * str1, char const * str2) {char * tmp = str1; assert (str1); assert (str2); while (* str2! = '\ 0') {* str1 ++ = * str2 ++;} return tmp;} int main () {char p [10] = "abc "; char * q = "defg"; printf ("% s \ n", my_strcpy (p, q); return 0 ;}
The running result is as follows: