A
Adrian Sch
Hello! I am doing some exercises from K&R 2nd edition and I seem to be stuck: Here's a working copy of a custom strcpy:
char *strcpy(char * s, char * t)
{
char x;
while(x=*s)
s++;
while((*s++=*t++))
;
return ;
}
int main(void)
{
char message[100]="string1";
char * message2="string2";
strcpy(message, message2);
printf("%s", message);
return 0;
}
And here's a version that doesn't work:
char *strcpy(char * s, char * t)
{
while(*s++)
;
while((*s++=*t++))
;
return ;
}
int main(void)
{
char message[100]="string1";
char * message2="string2";
strcpy(message, message2);
printf("%s", message);
return 0;
}
It seems that for some reason the while(*s++) is 0 so it doesn't increment. Why? What am I missing here?
char *strcpy(char * s, char * t)
{
char x;
while(x=*s)
s++;
while((*s++=*t++))
;
return ;
}
int main(void)
{
char message[100]="string1";
char * message2="string2";
strcpy(message, message2);
printf("%s", message);
return 0;
}
And here's a version that doesn't work:
char *strcpy(char * s, char * t)
{
while(*s++)
;
while((*s++=*t++))
;
return ;
}
int main(void)
{
char message[100]="string1";
char * message2="string2";
strcpy(message, message2);
printf("%s", message);
return 0;
}
It seems that for some reason the while(*s++) is 0 so it doesn't increment. Why? What am I missing here?