strcpy () function of Self realization __ function

Source: Internet
Author: User
Tags assert

strcpy () This function should be used by everyone, today, we will implement this function

int main (void)
{
	char arr1[20];
	Char arr2[] = "Hello world!";
	char *arr2 = NULL;

	my_strcpy (arr1, arr2);

	printf ("%s", arr1);
	System ("pause");
	return 0;
}

As above, first set the overall framework, and then start to write the my_strcpy function
char * my_strcpy (char * dest, const char * src)	//const make SRC Unable to do the left value to prevent while Dest and SRC write anti-
{
	char *ch = dest;
 
  assert (dest!= NULL);						Assertion helps debug
	assert (src!= NULL);
	while (*dest++ = *src++)					The//while is the simplest	
		;
	return dest;								Chained access printf ("%s", my_strcpy (dest, SRC));

 
As above, the comments are very detailed. It is worth mentioning that the assert (), which is called an assertion, means that when its condition is false, the program crashes and then gives a hint, which can help us debug the program well. To use this assertion, we want to include the header file on it: #include <assert.h>.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.