473,491 Members | 2,179 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 11507
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
2809
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
12002
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
2770
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
14292
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
1653
by: mahendra23 | last post by:
Can u please tell me the difference between strcpy() and memcpy().
5
18763
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
2769
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
2250
by: sreevidya1983 | last post by:
i want to know the difference between memcpy and strcpy please provide examples
3
2449
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.";
0
7115
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
6858
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
7360
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
5451
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4881
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4578
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
3076
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1392
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
633
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.