[C] strcpy of library function

Copy string

  Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).

  To avoid overflows, the size of the array pointed by destination shall be long enough to contain the same C string as source (including the terminating null character), and should not overlap in memory with source.

   The above content is the introduction of the strcpy function on the C++ official website. It can be seen that the strcpy function is used to copy strings. It copies the C string pointed to by source to the array pointed to by the target, including the terminating null character ('\0'), Copying ends when '\0' is encountered. In the process of copying, it should be noted that the size of the array pointed to by the target should be long enough to contain a string of the same size as the source string, and the target string cannot be a constant string such as "abc", because such Strings cannot be written, only read, and most importantly, the target string cannot overlap the source string.

   Next, I will explain how to write high-quality code by introducing several methods for implementing the strcpy function:

/*
* Function name: MyStrcpy
*
* Function: copy the content of the source string
*
* Entry parameters: dest, src
*
* Export parameter: dest
*
* Return type: void
*/

void MyStrcpy (char * dest, char * src)
{
	while ('\0' != (*src))
	{
		* dest = * src;
		src++;
		dest ++;
	}

	//When the loop jumps out *src=='\0', assign '\0' to *dest
	* dest = * src;

	return;
}

   Disadvantage: The statement inside the while loop is too cumbersome.

   Improvement: Optimize the statement inside the while loop to one sentence.

void MyStrcpy (char * dest, char * src)
{
	while ('\0' != (*src))
	{
		//The postfix ++ has a higher priority than *, and the postfix ++ is the first assignment plus 1,
		//So the first assignment is the original character of src (*src), after the end of an assignment
		// src + 1, dest + 1
		* dest ++ = * src ++;
	}

	//When the loop jumps out *src=='\0', assign '\0' to *dest
	* dest = * src;

	return;
}

   Disadvantage: *src should be divided into two cases ('\0' != *src and '\0' == *src) to discuss.

   Improvement: Optimize the statement inside the while loop as the loop condition.

void MyStrcpy (char * dest, char * src)
{
	while (*dest++ = *src++)
	{
		;
	}

	return;
}

   Insufficient: The validity of the pointer is not judged.

   Improvement: Check pointer validity.

void MyStrcpy (char * dest, char * src)
{
	if ((NULL != dest) && (NULL != src))
	{
		return;
	}
	else
	{
		while (*dest++ = *src++)
		{
			;
		}
	}
}

   Disadvantage: The above code needs to determine whether the pointer is empty every time.

   Improvement: Add assert assertion optimization.

void MyStrcpy (char * dest, char * src)
{
	assert((NULL != dest) && (NULL != src));

	while (*dest++ = *src++)
	{
		;
	}

	return;
}

   Disadvantage: If the order of dest and src is reversed, the program will only report an error during operation.

   Improvement: Make sure that the content of the source string src cannot be modified (add const), so that when writing inverse dest and src, an error will be reported when compiling.

void MyStrcpy(char * dest, const char * src)
{
	assert((NULL != dest) && (NULL != src));

	while (*dest++ = *src++)
	{
		;
	}

	return;
}

   Disadvantage: The above code cannot be passed into the function as a parameter.

   Improvement: Modify the function return type to char * to achieve chained access.

/*
* Function name: MyStrcpy
*
* Function: copy the content of the source string
*
* Entry parameters: dest, src
*
* Export parameter: dest
*
* return type: char *
*/

char * MyStrcpy (char * dest, const char * src)
{
	char *cp = dest;

	assert((NULL != src) && (NULL != cp));
		
	while (*cp++ = *src++)
	{
		;
	}

	return dest;
}

   How to write high quality code?

1. Use assert;

2. Try to use const;

3. Good coding style;

4. Necessary notes.

main function

#define _CRT_SECURE_NO_WARNINGS 1

/*
* Copyright (c) 2018, code farmer from sust
* All rights reserved.
*
* File name: MyStrcpy.c
* Function: Implement library function strcpy
*       char * strcpy ( char * destination, const char * source )
*
* Current version: V1.0
* Author: sustzc
* Completion date: April 20, 2018 11:37:28
*/

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

int main(void)
{
	char str1[10] = "abc";
	char *str2 = "123";

	printf("str1: %s\n", str1);
	printf("str2: %s\n", str2);
	printf("copy after,str1: %s\n", MyStrcpy(str1, str2));

	return 0;
}

output result


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325389055&siteId=291194637