strcpy and memcpy

Source: Internet
Author: User

1. Inconsist length.

Char a3[2];

Char *a = "Itis"

strcpy (A3, a); It is wrong. A3 would be correct, but a is missing.

memcpy (A3, A, sizeof(char) * 2); It would is correct for both A3 and a.

Char a2[2];

Char a3[2];

strcpy (A2, "aaaaaaaaaaaaaaaaaaaaa");

memcpy (A3,"AAAAAAAAAAAAAAAAAAAAA", sizeof(Char) * 2);

We'll get correct results only for A3

2. Not string Type

 please use strcpy

C language Functions Edit

Prototype declaration: extern Char *strcpy (char* dest, const char *src); header file: #include <string.h> and #include <stdio.h> Function: Copy a string starting from the SRC address with a null terminator to the address space beginning with dest: src and dest memory areas cannot overlap and dest must have enough space to accommodate the SRC string. Returns a pointer to the dest.2Typical implementationsEdit?
12345678910111213 /***********************C语言标准库函数strcpy的一种典型的工业级的最简实现*返回值:目标串的地址。*对于出现异常的情况ANSI-C99标准并未定义,故由实现者决定返回值,通常为NULL。*参数:des为目标字符串,source为原字符串*/ charstrcpy(char* des,constchar* source) { char* r=des; while((*(des++)=*(source++))!=‘\0‘); returnr; } /*while((*des++=*source++));的解释:赋值表达式返回左操作数,所以在赋值NULL后,循环停止*/
3Application ExamplesEditThe prototype of the known strcpy function is: char * strcpy (char * strdest,const char * strsrc); ⒈ does not call library functions to implement strcpy functions. ⒉ explains why you want to return char *. Explanation ⒈strcpy Implementation Code char * strcpy (char * strdest,const char * strsrc) {if ((null==strdest) | | (NULL==STRSRC)) [1]throw "Invalid argument (s)"; [2]char * strdestcopy = strdest; [3]while (*strdest++=*strsrc++)! = ' + '); [4]return strdestcopy;} The wrong approach: [1] (A) does not check the validity of pointers, indicating that the respondents do not pay attention to the robustness of the code. (B) Use when checking the validity of pointers (! strdest) | | (!STRSRC)) or (! (STRDEST&AMP;&AMP;STRSRC)), stating that the respondents did not have a deep understanding of the implicit conversion of the type in C language. In this case, the conversion of char * to bool is a type implicit conversion, although the functionality is flexible, but it is more likely to lead to increased error probability and higher maintenance costs. So C + + specifically adds bool, true, and false three keywords to provide a more secure conditional expression. (C) Use when checking the validity of pointers ((strdest==0) | | (strsrc==0)), stating that the respondents did not know the benefits of using constants. Using literal constants directly, such as 0 in this example, reduces the maintainability of the program. 0 Although simple, but there may be many places in the program to check the pointer, in case of a clerical error, the compiler can not find that the generated program contains logic errors, it is difficult to exclude. Instead of using null instead of 0, the compiler checks if a spelling error occurs. [2] (A) return new string ("Invalid argument (s)"), stating that the user does not know the purpose of the return value at all, and that he is not alert to memory leaks. It is dangerous to return the memory allocated in the function body from a function, and he throws the obligation to release memory to unsuspecting callers, in the vast majority of cases, the caller does not release memory, which results in a memory leak. (B) return 0, stating that the answer does not have an abnormal mechanism. It is possible for the caller to forget to check the return value, and the caller may not be able to check the return value (see following chain expression). Paranoia allows the return value to assume the dual function of returning the correct value and outliers, often with the result that both functions fail. Should be replaced by throwing an exceptionReturns a value that mitigates the burden on the caller, makes the error not to be ignored, and enhances the maintainability of the program. 2. Why should I return char *?

Returning the original value of DST enables a function to support chained expressions.

The form of a chain expression is as follows:

int L=strlen (strcpy (STRA,STRB));

Another example:

char * stra=strcpy (new CHAR[10],STRB);

It is wrong to return the original value of STRSRC.

One is that the source string is definitely known and it doesn't make sense to return it.

Second, it is not possible to support an expression that is shaped like the next.

The third, the const char * as char * return, the type does not match, compile error.

[3] (A) Forget to save the original strdest value, indicating that the answer is not strict logic. [4] (A) The loop is written as while (*strdestcopy++=*strsrc++), with [1] (B). (B) The loop is written in the while (*strsrc!= ') *strdest++=*strsrc++, which indicates that the examination of the boundary condition by the answer is poor. After the loop body ends, the end of the strdest string is not correctly appended with '/'. ⒉ returns the original value of strdest to enable the function to support chained expressions, adding "added value" to the function. Functions of the same function, if it can reasonably improve the availability of natural more ideal. The form of a chain expression is as follows: int Ilength=strlen (strcpy (STRA,STRB)), and char * stra=strcpy (new CHAR[10],STRB); The original value of the return strsrc is incorrect. One is that the source string is definitely known and it doesn't make sense to return it. Second, it is not possible to support an expression that is shaped like the next. Third, in order to protect the source string, the formal parameter with the const limit STRSRC content, the const char * as char * return, the type does not match, compile error. In the above statement, the Loop statement while ((*strdestcopy++=*strsrc++)! = ' \ ") is more difficult to understand and can be interpreted as the following operation. First: while (1) {char temp; *strdestcopy = *strsrc; strdestcopy++; strsrc++; temp = *strsrc;if (' + = temp ') break;} The second type: while (*strsrc! = ') ') {*strdestcopy = *strsrc;strdestcopy++;strsrc++;} *strdestcopy = *strsrc++; that is, while (*strsrc! = ') {*strdestcopy++ = *strsrc++;} *strdestcopy= ' + '; use example: //instance 1: Copy a string into a character array that is long enough. In this example, the character array is a and the length is 20. //Cons: If the array is not long enough to accommodate the entire string, the program runs crashing. #include <iostream> #include <stdlib.h>using namespace Std;char * strcpy (char * strdest, const char * strsrc) {C Har * strdestcopy = strdest;if ((null==strdest) | | (NULL==STRSRC)) Throw "Invalid argument"; while ((*strdest++=*strsrc++)! = ') '; return strdestcopy;} void Main (int argc, char * argv[]) {char a[20], c[] = "I am teacher!"; try{strcpy (a,c);} catch (char* strinfo) {cout << strinfo << endl;exit (-1);} cout << a << Endl;} //Instance 2: Preset Two-character pointer, one pointer to string, and the other to null, copied during program run. #include <iostream>using namespace Std;char *strcpy (char *strdes, const char *STRSRC); function declaration int Main () {const char *strsrc= "HelloWorld";; Char *strdes=null;strdes=strcpy (STRDES,STRSRC);cout<< "strsrc=" <<strSrc<<endl;cout<< " Strdes= "<<strdes<<endl;if (strdes!=null) {free (strdes); strdes=null;} return 0;} Char *strcpy (char *strdes, const char *strsrc) {assert (strsrc!=null);//If STRSRC is NULL, an exception is thrown. strdes= (char *) malloc (strlen (STRSRC) +1); One more space to store the string Terminator ' *p=strdes;while ' char (*strsrc!= ') {*p++=*strsrc++;} *p= ' + '; return strdes;} Differences with strncpy The first case: 1234char* p= "How is it?"; Char name[20]= "ABCDEFGHIJKLMNOPQRS"; strcpy (name,p); Name changed to "How is it?" "====> Right!" strncpy (name,p, sizeof (name));//name changed to "How is you?" =====> right! Subsequent characters are set to null the second case: 123456char* p= "How is it?"; Char name[10];strcpy (NAME,P); Target string length is less than source string, error! Name[sizeof (name) -1]= ' + '; And the previous combination to compensate for the results, but this is not advisable because the previous error handling method is not deterministic strncpy (name,p,sizeof (name)); The length of the source string is greater than the length of the specified copy sizeof (name), note that in this case the target string is not automaticallySurface Plus ' name[sizeof ' (name) -1]= ' + '; And a combination of the previous step to compensate for the results

strcpy and memcpy

Related Article

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.