Stracpy standard writing method
Example code:
// : Define the entry point of the console application.// #include "" using namespace std; /* * Description: String copy version 1 * Parameters: dest target address, src source address * Return: Return the copied address; if there is an error or overlap, there is no definition * Exception: String overflow may occur, and the space occupied by dest is not as large as that of src. */ char *strcpy_v1(char *dest , const char *src) { //When debugging, use assertions and entry detection assert( (dest!=NULL) && (src!=NULL) ); //Note that the memory here points to the memory where the parameter dest is located, not the stack memory, so it can be returned in the function char *to = dest; //The main operation is completed in the while condition while( (*dest++ = *src++)!='\0') { NULL; } //Return the first address of the copy string to facilitate connection, such as strlen(strcpy(dest,"hello")) return to; } /* * Description: String copy version 2 * Parameters: dest target address, src source address * Return: Return the copied address; if there is an error, no definition * Exception: String overflow may occur, and the space occupied by dest is not as large as that of src. */ char *strcpy_v2(char *dest , const char *src) { char *d = dest; char c; while((c=*src++) != '\0') { *(dest++)=c; } *dest='\0'; return d; } /* * Description: String copy version 2 (Can you find out the cause of the error) * Parameters: dest target address, src source address * Return: Return the copied address; if there is an error, no definition * Exception: String overflow may occur, and the space occupied by dest is not as large as that of src. */ char *strcpy_v2_error(char *dest , const char *src) { char *d = dest; char c; while((c=*src++) != '\0') { *(d++)=c; } *d='\0'; return d; } /* * Description: String copy version 3 * Parameters: dest target address, src source address * Return: Return the copied address; if there is an error, no definition * Exception: String overflow may occur, and the space occupied by dest is not as large as that of src. */ char *strcpy_v3(char *dest , const char *src) { char *d = dest; char c; while(*src) *dest++ = *src++; *dest='\0'; return d; } /* * Description: String copy version 4 * Parameters: dest target address, src source address * Return: Return the copied address; if there is an error, no definition * Exception: String overflow may occur, and the space occupied by dest is not as large as that of src. */ char *strcpy_v4(char *dest , const char *src) { char *d = dest; char c; while( (*dest = *src)!='\0') { src++; dest++; } *dest='\0'; return d; } /* * Description: String copy version 5 * Parameters: dest target address, src source address * Return: Return the copied address; if there is an error, no definition * Exception: String overflow may occur, and the space occupied by dest is not as large as that of src. restrict keyword qualified strings cannot overlap. */ char *strcpy_v5(char* _restrict dest , const char* _restrict src) { char *d = dest; char c; while( (*dest = *src)!='\0') { src++; dest++; } *dest='\0'; return d; } /* * Description: String copy version 6 * Parameters: dest target address, src source address * Return: Return the copied address; if there is an error, no definition * Exception: String overflow may occur, and the space occupied by dest is not as large as that of src. restrict keyword qualified strings cannot overlap. */ char *strcpy_v6(char* _restrict dest , const char* _restrict src) { char *d = dest; char c; while(*dest++=*src++); return d; } int _tmain(int argc, _TCHAR* argv[]) { char buf[512]; char *buf2 = (char *)calloc(50,sizeof(char)); char *buf3 = (char *)malloc(50*sizeof(char)); char *buf5 = (char *)malloc(50*sizeof(char)); char *buf6 = (char *)malloc(50*sizeof(char)); printf("using strcpy_v1,the string 'Hello,World'\'s length is : %d\n",strlen(strcpy_v1(buf,"Hello,World"))); printf("using strcpy_v2,the string 'This is the best age'\'s length is : %d\n",strlen(strcpy_v2(buf2,"This is the best age"))); printf("using strcpy_v2,the string 'This is the best age'\'s length is : %d\n",strlen(strcpy_v2_error(buf2,"This is the best age"))); printf("using strcpy_v3,the string 'This is the best age'\'s length is : %d\n",strlen(strcpy_v3(buf3,"This is the best age"))); printf("using strcpy_v5,the string 'This is the best age'\'s length is : %d\n",strlen(strcpy_v5(buf5,"This is the best age"))); printf("using strcpy_v6,the string 'This is the best age'\'s length is : %d\n",strlen(strcpy_v6(buf6,"This is the best age"))); system("pause"); return 0; }
Thank you for reading, I hope it can help you. Thank you for your support for this site!