[C language] Simulation implementation library function: strcpy

Table of contents

Learn about function functions through the cplusplus website:

Assert the use of assert:

About const:

What you should know most about this article:


Learn about function functions through the cplusplus website:

To simulate and implement library functions, first we need to understand the parameters of this function, the return value of the function, the function of the function, etc. We can query through cplusplus.com - The C++ Resources Network . After analysis, we can know:

 Write the code according to the corresponding requirements, and the const inside is said separately

First edition:

void my_strcpy(char* dest, char* src)
{
	while (*src != '\0')
	{
		*dest = *src;
		dest++;
		src++;
	}
	*dest = *src;// \0 的拷贝
}

int main()
{
	char arr1[] = "hello bit";
	char arr2[20] = "xxxxxxxxxxxxx";
	my_strcpy(arr2, arr1);
	printf("%s\n", arr2);
	return 0;
}

After a simple analysis, the function of the function has been basically realized here, but in the library function, the return value of strcpy is the pointer of the destination array, and we will optimize it for the first time

second edition:

char* my_strcpy(char* dest, char* src)
{
	char* ret = dest;//保存指针初始位置,方便后面返回值
	while (*src != '\0')
	{
		*dest = *src;
		dest++;
		src++;
	}
	*dest = *src;// \0 的拷贝
	return ret;
}

int main()
{
	char arr1[] = "hello bit";
	char arr2[20] = "xxxxxxxxxxxxx";
	printf("%s\n", my_strcpy(arr2, arr1));
	return 0;
}

It should be noted that since the src and dest pointers keep changing positions during the subsequent copy process, in order to finally return the destination string, we need to save the starting position of the destination string before changing the pointer.

Assert the use of assert:

For the strcpy function, for the safety of the function, we need to avoid null pointers when passing parameters. At this time, we can introduce the concept of assertion. When using assertions, we need to refer to the header file assert.h. The specific method of use is assert( requirements), for example, in this question, we can use assert (dest!=NULL) like this, if the assert finds that the conditions in the brackets are not met at this time, the assert will report an error, ensuring the safety of the function and avoiding unknown errors in the function.

Third edition:

#include<assert.h>

char* my_strcpy(char* dest, char* src)
{
    //断言
    assert(dest!=NULL);
    assert(src!=NULL);
	char* ret = dest;//保存指针初始位置,方便后面返回值
	while (*src != '\0')
	{
		*dest = *src;
		dest++;
		src++;
	}
	*dest = *src;// \0 的拷贝
	return ret;
}

int main()
{
	char arr1[] = "hello bit";
	char arr2[20] = "xxxxxxxxxxxxx";
	printf("%s\n", my_strcpy(arr2, arr1));
	return 0;
}

About const:

 The concepts are as follows:

 Let us give you a few examples:

void test1()
{
	int n = 10;
	int m = 20;
	int* p = &n;
	*p = 20;//ok
	p = &m;//ok
}

void test2()
{
	int n = 10;
	int m = 20;
	const int* p = &n;//const放在*前,表示指针p指向的内容不可以被修改,但指针p不影响
	*p = 20;//err
	p = &m;//ok
}

void test3()
{
	int n = 10;
	int m = 20;
	int* const p = &n;//const放在*后,表示指针p不可以被修改,但指针p指向的内容不影响
	*p = 20;//ok
	p = &m;//err
}

Knowing the usage of const, we can optimize again.

Fourth edition:

#include<assert.h>

char* my_strcpy(char* dest, const char * src)
{
	char* ret = dest;
	//断言
	assert(dest != NULL);
	assert(src != NULL);

	while (*dest++ = *src++) 
		;//空语句
	return ret;
}


int main()
{
	char arr1[] = "hello bit";
	char arr2[20] = "xxxxxxxxxxxxx";
	printf("%s\n", my_strcpy(arr2, arr1));
	return 0;
}

What you should know most about this article:

(1) The use of assert assertion, after learning the use of assertion, it is recommended to apply it in the future, which can avoid some unexpected errors in the function and find the bug directly.

(2) The use of const, clever use of const, can protect some values ​​that you do not want to be changed.

(3) The use of websites such as cplusplus can query library functions.

Guess you like

Origin blog.csdn.net/2301_77112634/article/details/130914939