C language - Simulate strcpy function (assert() function, usage of const)

Table of contents

1. Simulate the strcpy function

1.1 assert() function

1.2 const usage

1.2.1 Role 1: const modified variable

1.2.2 Function 2: const modified pointer

1.3 Simulation strcpy function code optimization 


1. Simulate the strcpy function

A mock function is not an implementation function, there is a difference! The simulation function must fully conform to the definition of this function, and the implementation function only needs to realize the function finally displayed by this function. The following is the code implementation:


#include<stdio.h>
#include<assert.h>

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

 	while (*src != '\0')
   {
	    *dest = *src;
		dest++;
		src++;
	}
	return ret;
}

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

1.1 assert() function

        If the dest and src pointers are null pointers (NULL) when passing parameters, the program cannot display the results, which requires the programmer to debug the code. The function of assert is to report an error in time when the pointer is empty, saving debugging time.

At this time, when a null pointer is passed into the simulation function, it is truncated by the assert function:

1.2 const usage

Function 1: const modifier variable

Function 2: const modified pointer

1.2.1 Role 1: const modified variable

 When the variable n is defined with const, then an error will be reported if n is modified later. In layman's terms, n has changed from a variable to a constant, and the constant cannot be modified.

#include<stdio.h>
int main()
{
	const int n = 0;
	n = 20;
	printf("%d\n",n);
	return 0;
}

1.2.2 Function 2: const modified pointer

This situation will be relatively complicated!

Modification before *: Indicates modifying the content pointed by the pointer, and the content pointed to by the pointer cannot be modified. But the pointer variable itself                        can be changed.

Modification after *:  means to modify the pointer variable itself, the content pointed to by the pointer variable cannot be modified, but the content pointed to by the pointer                         can be modified.

 Modifications before *:

#include<stdio.h>
int main()
{
	int n = 10;
	int m = 20;

	const int* p = &n;
	*p = m;//错误,指针指向的内容不得被改变
	p = &m;//正确,指针变量指向的内容可以改变

	printf("%d\n", n);
	return 0;
}

Modification after *: 

#include<stdio.h>
int main()
{
	int n = 10;
	int m = 20;

	int* const p = &n;
	*p = m;//正确,指针指向的内容可以被修改
	p = &m;//错误,指针变量指向的内容不得被修改

	printf("%d\n", n);
	return 0;
}

1.3 Code optimization 


#include<stdio.h>
#include<assert.h>

char* my_strcpy(char* dest, const char* src)
{
	assert(dest && src);//断言
	char* ret = dest;
//
    while (*dest++ = *src++)//此语句直接实现了判断、赋值和自增
    {
	    ;
    }
//
    return ret;
}

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

Guess you like

Origin blog.csdn.net/weixin_59174190/article/details/122551166