Click to See Complete Forum and Search --> : ->Re: What wrong!!!(Mike)


Toms
09-06-2001, 12:00 PM
Thanks a lot.
but i still dont know what you mean "typedef void (*COPY)(...)
also , i want to know
**
void copy1(char *s1 , const char *s2)
{
while(*s2 != '\0')
{
*s1 = *s2 ;
s1++;
s2++;
}
// choose one of them only
>> *(s1++) = '\0' ; // print "12345678"
>> *(++s1) = '\0' ; // print "12345678" plus a binary code
}
**
why the last coding *(++s1) = '\0' will print "12345678" plus a binary code
what i want just to print "12345678"

Thanks a lot
Toms

"www.exontrol.com" <mike@exontrol.com> wrote:
>
>You need typedef void (*COPY)( ... )
>
>Mike
>www.exontrol.com
>"Toms" <tomsng@sinaman.com> wrote:
>>
>>what wrong about the following coding.
>>
>>void copy1(char * , const char *) ;
>>int main()
>>{
>> char string1[10] , string2[] = "12345678";
>> copy1(string1,string2)
>> cout << "string1 is " << string1 << endl;
>> return 0 ;
>>}
>>
>>void copy1(char *s1 , const char *s2)
>>{
>> while(*s2 != '\0')
>> {
>> *s1 = *s2 ;
>> s1++;
>> s2++;
>> }
>> // choose one of them only
>> *(s1++) = '\0' ; // print "12345678"
>> *(++s1) = '\0' ; // print "12345678" plus a binary code
>>}
>>
>>Can anyone help me about this.
>>Thanks a lot
>>Toms
>>
>>
>

marilyn
09-06-2001, 07:31 PM
At the end of your while loop your s1 and s2 have both already been
incremented, so s1 will have been incremented 1 past the last char.
When you do *(++s1) = '\0', the pre-increment means s1 will first be
incremented again, now 2 past the last char, and the value '\0' assigned to
that. So your string will have
"12345678<GARBAGE>0"

"Toms" <Tomsng@sinaman.com> wrote in message
news:3b979da0$1@news.devx.com...
>
> Thanks a lot.
> but i still dont know what you mean "typedef void (*COPY)(...)
> also , i want to know
> **
> void copy1(char *s1 , const char *s2)
> {
> while(*s2 != '\0')
> {
> *s1 = *s2 ;
> s1++;
> s2++;
> }
> // choose one of them only
> >> *(s1++) = '\0' ; // print "12345678"
> >> *(++s1) = '\0' ; // print "12345678" plus a binary code
> }
> **
> why the last coding *(++s1) = '\0' will print "12345678" plus a binary
code
> what i want just to print "12345678"
>
> Thanks a lot
> Toms
>
> "www.exontrol.com" <mike@exontrol.com> wrote:
> >
> >You need typedef void (*COPY)( ... )
> >
> >Mike
> >www.exontrol.com
> >"Toms" <tomsng@sinaman.com> wrote:
> >>
> >>what wrong about the following coding.
> >>
> >>void copy1(char * , const char *) ;
> >>int main()
> >>{
> >> char string1[10] , string2[] = "12345678";
> >> copy1(string1,string2)
> >> cout << "string1 is " << string1 << endl;
> >> return 0 ;
> >>}
> >>
> >>void copy1(char *s1 , const char *s2)
> >>{
> >> while(*s2 != '\0')
> >> {
> >> *s1 = *s2 ;
> >> s1++;
> >> s2++;
> >> }
> >> // choose one of them only
> >> *(s1++) = '\0' ; // print "12345678"
> >> *(++s1) = '\0' ; // print "12345678" plus a binary code
> >>}
> >>
> >>Can anyone help me about this.
> >>Thanks a lot
> >>Toms
> >>
> >>
> >
>