What is the difference between strcpy and memcpy in c
Improve this answer. Hello, I know that post is old, but I have two questions regarding it. Besides I can see NO dot at all Thanks in advance for Your answer! In a printf statement, "x" means "base 16". Yann Ramin Yann Ramin Sachin: Initialize p and t to something all blanks, for example , then after copying, compare p[3] to t[3].
The strcpy didn't go beyond p[2] , where it found the null character, but the memcpy as directed copied five characters. Minor nit-pick: strcpy stops when it encounters the NUL character one "L". NULL two "L"s is a compile-time constant for a pointer guaranteed not to point to any valid object. Brian Viswesn Viswesn 4, 1 1 gold badge 26 26 silver badges 38 38 bronze badges.
I don't know where did you see strcpy being deprecated. RohanBari It's a royal outrage if this is someone with only MSVC experience speaking about " deprecated functions " all over again for the billionth time — An Ant. Jeremy Friesner Jeremy Friesner Jaspar L. Ardent Coder 3, 9 9 gold badges 22 22 silver badges 45 45 bronze badges. Sign up or log in Sign up using Google. That is, strcpy is only used for string copying, and it not only copies the content of the string, but also copies the end of the string.
There are three main differences between strcpy and memcpy:. The copied content is different. The method of copying is different. Different uses. Usually strcpy is used when copying strings, and memcpy is used to copy other types of data. Both memcpy and memmove are library functions in the C language. The method of copying is different.
The purpose is different. Usually strcpy is used when copying strings, and memcpy is generally used when copying other types of data. Another more convenient thing is the operation of the structure. Memset can easily clear a variable or array of structure type:. Generally, to clear the structure, use the following method:.
The written test question I encountered today was to implement strcpy and memcpy. I didn't make it at that time. I searched the Internet and found that it was a question that had been published on Taobao in less than a few years.
Although this function is flexible, it more often leads to an increase in the probability of errors and an increase in maintenance costs. Use literal constants directly such as 0 in this example.
Will reduce the maintainability of the program. In case of clerical errors, the compiler cannot find out.
0コメント