473,467 Members | 1,991 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

memcpy vs strcpy

5 New Member
When "memcpy" should be used over "strcpy" and viceversa?

Thanks
Nov 20 '06 #1
2 11503
horace1
1,510 Recognized Expert Top Contributor
When "memcpy" should be used over "strcpy" and viceversa?

Thanks
strcpy is used to copy null terminated strings, e.g.
Expand|Select|Wrap|Line Numbers
  1. char *strcpy(char *destination, const char *source);
  2.  
copy string source to string destination stopping after the terminating null-character is copied; return a pointer to destination,e.g.
Expand|Select|Wrap|Line Numbers
  1.     char ch1[10]="hello";
  2.     char ch2[10];
  3.     strcpy(ch2, ch1);
  4.  
"hello" + terminating null is copied from ch1[] to ch2[]

memcpy() is an efficent way to copy memory areas
Expand|Select|Wrap|Line Numbers
  1. void *memcpy(void *destination, const void *source, size_t number);
  2.  
copy at most number bytes from source to destination; return a pointer to destination

you can use it as a general tool to copy areas of memory, e.g.
Expand|Select|Wrap|Line Numbers
  1.     int a[10] = {1,2,3,4,5,6,7,8,9,10};
  2.     int b[10]={0};
  3.     memcpy(b, a, sizeof(int)* 10);
  4.  
10 ints are copied from a[] to b[]

for discussion on efficency of memcpy see
http://www.embedded.com/showArticle.jhtml?articleID=19205567
Nov 20 '06 #2
slrj
5 New Member
Horace1,

Thanks for the explanation with code examples.


Thanks.
Nov 20 '06 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: cylin | last post by:
Dear all, When I use memcpy second time, it seems to be no use. There should be two "Hello,world!" strings in this buffer. I don't know what's wrong? Please help, thanks. Regards, cylin....
35
by: Christopher Benson-Manica | last post by:
(if this is a FAQ or in K&R2, I didn't find it) What parameters (if any) may be 0 or NULL? IOW, which of the following statements are guaranteed to produce well-defined behavior? char src;...
16
by: Jeff | last post by:
Im trying to memcpy a buffer from a filled in simple structure. When I memcpy and then print the resulting buffer, I see 7 locations that have junk before my data starts. My data structure is: ...
3
by: naren | last post by:
Iam not getting the correct pros and cons of the strcpy() and memcpy() some where i read for short strings strcpy is faster and for large strings memcpy is faster.. in strcpy() there is a single...
2
by: mahendra23 | last post by:
Can u please tell me the difference between strcpy() and memcpy().
5
by: xdevel | last post by:
Hi, anyone can make me an example where memmove does not cause a memory overlapping and where memcpy do it? Thanks
14
by: somenath | last post by:
Hi All, I am trying to understand the behavior of the memcpy and memmove. While doing so I wrote a program as mentioned bellow . #include<stdio.h> #include<stdlib.h> #include<string.h> ...
1
by: sreevidya1983 | last post by:
i want to know the difference between memcpy and strcpy please provide examples
3
by: mjbauer95 | last post by:
What is the difference between memcpy() and just using the = symbol, as in: char * str1; str1 = "TEST."; char * str2; str2 = str1; vs. char * str1; str1 = "TEST.";
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.