In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "how to use strcpy functions in C language", so the editor summarizes the following, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use strcpy functions in C language" article.
The string functions in C language are as follows
Get string length
Strlen
String function with unlimited length
Strcpy
Strcat
Strcmp
String function with limited length
Strncpy
Strncat
Strncmp
String lookup
Strstr
Strtok
Error message report
Strerror
Let's see how to implement them.
String function strcpy with unlimited length
Let's see what the document says, as follows
Strcpy document
Char * strcpy (char * destination, const char * source)
Copy string
String copy (string copy)
Copies the C string pointed by source into the array pointed by destination, including the terminating null character (and stopping at that point).
Copy the C string pointed by the character pointer source to another character array, which is pointed to by the character pointer destination
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.
To avoid overflow, the character array pointed to by destination needs to be long enough to contain the source string (including'\ 0')
To sum up, we can know
The'\ 0' in the source string is copied to the destination space, and the source string must end with'\ 0'.
The target space must be large enough to ensure that the source string can be stored.
How to make a copy? Int main () {char arr1 [] = "abcdefghi"; char arr2 [] = "bit"; / / copy the contents of arr2 to arr1 / / strcpy (arr1, arr2); / / how to copy? My_strcpy (arr1, arr2); printf ("% s\ n", arr1); return 0;} implementation
It is a good habit to assert that the pointer is not empty.
/ / char* my_strcpy (char* dest, char* src) / / src plus const, why? Because we only need to copy and do not need to change the source string to prevent modification, we add const to modify char* my_strcpy (char* dest, const char* src) {assert (dest! = NULL); assert (src! = NULL); while (* src! ='\ 0') {* dest = * src; dest++; src++ } * dest = * src; / /'\ 0' / / returns the starting address of the destination space return dest;}
Copy the source string to the destination space, look for'\ 0', execute * dest = * src if it is not'\ 0', assign the source character to the destination space, and then both pointers are offset back, that is, both are + +. When * src is'\ 0', indicating that the source string has reached the end, exit the loop, directly assign'\ 0' to * dest, and finally return dest
Can be optimized as follows
Char* my_strcpy (char* dest, const char* src) {assert (dest! = NULL); assert (src! = NULL); / / optimize while (* src! ='\ 0') {* dest++ = * src++;} * dest = * src; / /'\ 0' / return the starting address of the destination space return dest;}
Of course, you can continue to optimize and become more concise, directly using * dest++ = * src++ as the judgment condition, while performing the operation, as follows
Char* my_strcpy (char* dest, const char* src) {assert (dest! = NULL); assert (src! = NULL); / / optimize / / copy the string pointed to by src to the space pointed to by dest, including'\ 0' char* rest = dest; while (* dest++ = * src++) { } / return the starting address of the destination space return rest;} the above is about the article "how to use strcpy function in C language". I believe you all have some understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about it, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 218
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.