@all: I have a basic doubt here...does not copying of zero character to string behave as null character?..meaning shuld not printf give an empty string when i tried printing str itself since first character is now '0' ?? am confused !
On Sat, Jul 9, 2011 at 1:05 AM, oppilas . <[email protected]> wrote: > 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 <[email protected] > > 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 >> <[email protected]>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 >>> <[email protected]>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 <[email protected]> >>>> 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 < >>>> [email protected]> >>>> > wrote: >>>> >> >>>> >> you are overwriting terminating null char!! >>>> >> >>>> >> On Fri, Jul 8, 2011 at 10:23 AM, rShetty <[email protected]> >>>> 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 [email protected]. >>>> >> > To unsubscribe from this group, send email to >>>> >> > [email protected]. >>>> >> > 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 [email protected]. >>>> >> To unsubscribe from this group, send email to >>>> >> [email protected]. >>>> >> 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 [email protected]. >>>> > To unsubscribe from this group, send email to >>>> > [email protected]. >>>> > 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 [email protected]. >>>> To unsubscribe from this group, send email to >>>> [email protected]. >>>> 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 [email protected]. >>> To unsubscribe from this group, send email to >>> [email protected]. >>> For more options, visit this group at >>> http://groups.google.com/group/algogeeks?hl=en. >>> >> >> >> >> -- >> Regards, >> Kamakshi >> [email protected] >> >> -- >> You received this message because you are subscribed to the Google Groups >> "Algorithm Geeks" group. >> To post to this group, send email to [email protected]. >> To unsubscribe from this group, send email to >> [email protected]. >> 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 [email protected]. > To unsubscribe from this group, send email to > [email protected]. > For more options, visit this group at > http://groups.google.com/group/algogeeks?hl=en. > -- "People often say that motivation doesn't last. Well, neither does bathing - that's why we recommend it daily." -- You received this message because you are subscribed to the Google Groups "Algorithm Geeks" group. To post to this group, send email to [email protected]. To unsubscribe from this group, send email to [email protected]. For more options, visit this group at http://groups.google.com/group/algogeeks?hl=en.
