There are few bugs in your code.
You are going to copy 10 characters in string str1, so the ideal size of str
should be 10+1(for null character).
Now when u used memcpy, then it perfectly copies initial 10 characters in
str1.
You can see it in you current output. But when u use %s for printing then it
keeps printing untill it encounters NULL character. So, the behavior of this
code is unpredictable. U can fix it by declaring char str[11]; and adding
NULL character at the end str[11]='\0'


http://codepad.org/C69G7Gzz
On Sat, Jul 9, 2011 at 12:01 PM, Kamakshii Aggarwal
<kamakshi...@gmail.com>wrote:

> in my compiler,its giving
> 0000 is rajeev
>
>  this is string 1
> 0000 is ra"
>
>
> On Fri, Jul 8, 2011 at 11:33 AM, rajeev bharshetty 
> <rajeevr...@gmail.com>wrote:
>
>> @Vishal : yes I can use strcpy, But What is the problem with memcpy().
>>
>>
>> On Fri, Jul 8, 2011 at 11:27 AM, Vishal Thanki <vishaltha...@gmail.com>wrote:
>>
>>> memcpy is usually used to copy data structures other than strings. Use
>>> strcpy for copying strings!!
>>>
>>> On Fri, Jul 8, 2011 at 11:25 AM, Navneet Gupta <navneetn...@gmail.com>
>>> wrote:
>>> > @Vishal, still the array str1 is also 10 bytes only, I know that memcpy
>>> > overwrites the null character but not sure then what is the correct
>>> approach
>>> > if i still want to use memcpy. Thoughts?
>>> >
>>> > On Fri, Jul 8, 2011 at 10:51 AM, Vishal Thanki <vishaltha...@gmail.com
>>> >
>>> > wrote:
>>> >>
>>> >> you are overwriting terminating null char!!
>>> >>
>>> >> On Fri, Jul 8, 2011 at 10:23 AM, rShetty <rajeevr...@gmail.com>
>>> wrote:
>>> >> > #include<stdio.h>
>>> >> > #include<string.h>
>>> >> > int main()
>>> >> > {
>>> >> >  char str[]="This is rajeev\n";
>>> >> >  char str1[10];
>>> >> >  memset(str,'0',4);
>>> >> >  printf("%s",str);
>>> >> >  memcpy(str1,str,10);
>>> >> >  printf("\n this is string 1\n");
>>> >> >  printf("%s\n",str1);
>>> >> >  return 0;
>>> >> > }
>>> >> >
>>> >> > Output is :
>>> >> >
>>> >> > 0000 is rajeev
>>> >> >
>>> >> >  this is string 1
>>> >> > 0000 is ra0000 is rajeev
>>> >> >
>>> >> >
>>> >> > it copies 10 characters from str to str1 so the printing 0000 is ra
>>> is
>>> >> > Ok what abt the repeating result?
>>> >> >
>>> >> > --
>>> >> > You received this message because you are subscribed to the Google
>>> >> > Groups "Algorithm Geeks" group.
>>> >> > To post to this group, send email to algogeeks@googlegroups.com.
>>> >> > To unsubscribe from this group, send email to
>>> >> > algogeeks+unsubscr...@googlegroups.com.
>>> >> > For more options, visit this group at
>>> >> > http://groups.google.com/group/algogeeks?hl=en.
>>> >> >
>>> >> >
>>> >>
>>> >> --
>>> >> You received this message because you are subscribed to the Google
>>> Groups
>>> >> "Algorithm Geeks" group.
>>> >> To post to this group, send email to algogeeks@googlegroups.com.
>>> >> To unsubscribe from this group, send email to
>>> >> algogeeks+unsubscr...@googlegroups.com.
>>> >> For more options, visit this group at
>>> >> http://groups.google.com/group/algogeeks?hl=en.
>>> >>
>>> >
>>> >
>>> >
>>> > --
>>> > Regards,
>>> > Navneet
>>> >
>>> >
>>> > --
>>> > You received this message because you are subscribed to the Google
>>> Groups
>>> > "Algorithm Geeks" group.
>>> > To post to this group, send email to algogeeks@googlegroups.com.
>>> > To unsubscribe from this group, send email to
>>> > algogeeks+unsubscr...@googlegroups.com.
>>> > For more options, visit this group at
>>> > http://groups.google.com/group/algogeeks?hl=en.
>>> >
>>>
>>> --
>>> You received this message because you are subscribed to the Google Groups
>>> "Algorithm Geeks" group.
>>> To post to this group, send email to algogeeks@googlegroups.com.
>>> To unsubscribe from this group, send email to
>>> algogeeks+unsubscr...@googlegroups.com.
>>> For more options, visit this group at
>>> http://groups.google.com/group/algogeeks?hl=en.
>>>
>>>
>>  --
>> You received this message because you are subscribed to the Google Groups
>> "Algorithm Geeks" group.
>> To post to this group, send email to algogeeks@googlegroups.com.
>> To unsubscribe from this group, send email to
>> algogeeks+unsubscr...@googlegroups.com.
>> For more options, visit this group at
>> http://groups.google.com/group/algogeeks?hl=en.
>>
>
>
>
> --
> Regards,
> Kamakshi
> kamakshi...@gmail.com
>
> --
> You received this message because you are subscribed to the Google Groups
> "Algorithm Geeks" group.
> To post to this group, send email to algogeeks@googlegroups.com.
> To unsubscribe from this group, send email to
> algogeeks+unsubscr...@googlegroups.com.
> For more options, visit this group at
> http://groups.google.com/group/algogeeks?hl=en.
>

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to