GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Programming  >  C
Go To First  |  Previous Question  |  Next Question 
 C  |  Question 418 of 453    Print  
strncpy() & memcpy() function in C
what is the difference between strncpy()(not strcpy) & memcpy() function in C?


  
Total Answers and Comments: 3 Last Update: July 18, 2008     Asked by: Muthupandi_s 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: balaji06nitc
 
strcpy is meant to copy only null-terminated strings. It is probably
: implemented to copy every byte until it encounters a #0.
: memcpy can copy any memory location. It is not bound by a
: null-terminated string. Since memcpy cannot determine the size of
: the data to be copied, it needs the programmer to provide that
: information.


That is correct. Note that there is also a function called strncpy() which copies n bytes. It is almost identical to memcpy(), with the difference that it adds null termination at the end of the target string.


Above answer was rated as good by the following members:
Muthupandi_s, jca_dips, rajani_vaddepalli15
June 18, 2008 08:29:39   #1  
balaji06nitc Member Since: June 2008   Contribution: 1    

strncpy() & memcpy() function in C
strcpy is meant to copy only null-terminated strings. It is probably
: implemented to copy every byte until it encounters a #0.
: memcpy can copy any memory location. It is not bound by a
: null-terminated string. Since memcpy cannot determine the size of
: the data to be copied it needs the programmer to provide that
: information.


That is correct. Note that there is also a function called strncpy() which copies n bytes. It is almost identical to memcpy() with the difference that it adds null termination at the end of the target string.

 
Is this answer useful? Yes | NoAnswer is useful 3   Answer is not useful 2Overall Rating: +1    
July 14, 2008 18:05:56   #2  
janhavibend Member Since: July 2008   Contribution: 5    

RE: strncpy() & memcpy() function in C
when I checked with strncpy and memcpy both are not terminating string with ' ' after copying when dst size is less than src size. otherwise both will terminate dst string with ' ' when sizeof(dst) > sizeof(src)
 
Is this answer useful? Yes | No
July 18, 2008 06:29:57   #3  
jca_dips Member Since: July 2008   Contribution: 3    

RE: strncpy() & memcpy() function in C

strncpy copies upto null or specified no. of bytes whichever is less. But memcpy overlooks null and copies all specified no. of bytes.
None of them adds null in destination string by itself.

#include<conio.h>
#include<memory.h>
#include<string.h>

void main()
{
char str1[50] "This is india: a wonderful country";
char str2[30];

printf(" s" str1);
strncpy(str2 str1 15);
//memcpy(str2 str1 15);
//str2[12] ' ';
printf("n s" str2);
printf("n c c" str2[12] str2[13]);

getch();

}

str1[8] is null. As strncpy copies upto null str[12] and str[13] takes value as blank.
In case of memcpy as all the specifed 15 bytes are copied in the destination string
str[12] takes 'i' and str[13] takes 'a' as values.


 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape