[C language] strcpy() function

Article directory

1. Introduction to the strcpy() function

Second, the specific use of the strcpy() function

3. Precautions for using the strcpy() function


1. Introduction to the strcpy() function

 strcpy() function: It is a function to copy a string to another space address, '\0' is the termination condition to stop copying, and '\0' will also be copied to the target space. The following is the declaration of the strcpy() function in the library:

char* strcpy(char* destination,const char* source);

   1. The parameters of the function:

  • char* destination---------the first address of the target string
  • const char* source------source address: the first address of the copied string, modified with const to avoid modifying the copied string

   2. The return value type of the function:

  • char*: Returns the first address of the target string 

Second, the specific use of the strcpy() function

scene one:

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>

int main()
{
	char arr[10] = "########";
	printf("%s\n", strcpy(arr,"hello"));

	return 0;
}

 output result

 

Scene two: 

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>

int main()
{
	char arr1[10] = "**********";
	char arr2[10] = "abcd";
	printf("%s\n", strcpy(arr1, arr2));
	return 0;
}

output result

 

3. Precautions for using the strcpy() function

1. The source character must end with '\0':

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>

int main()
{
	char arr1[10] = "**********";
	char arr2[] = { 'a','b','c','d' };
	printf("%s\n", strcpy(arr1,arr2));

	return 0;
}

The code here will be wrong, '\0' is the termination condition to stop copying, the content stored behind the memory space where the arr2 character array is located is unknown, copying will not stop until '\0' is encountered, which will lead to out-of-bounds access , the program will have problems.

2. The target space must be large enough to ensure that the source string can be placed

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>

int main()
{
	char arr1[5] = "*****";
	char arr2[] = "hello world";
	printf("%s\n", strcpy(arr1,arr2));

	return 0;
}

 Although the copy is successful and the result is output here, the program crashes. The target space is too small to place the copied source string, which will cause overflow

3. The target space must be variable

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>

int main()
{
	char* str1 = "hello world";
	char str2[10] = "*********";
	printf("%s\n", strcpy(str1,str2));

	return 0;
}

There is also an error in the program here. str1 points to a constant string, which cannot be modified, and the target space must be modifiable, because the copied string must be placed in the target space. The source string can be modified or not, because the second parameter of the strcpy function has been modified with the const keyword, which ensures that it will not be modified during the copy process.

Guess you like

Origin blog.csdn.net/weixin_47970952/article/details/126163736