-
pointer confusion
hi,
What exactly happens in this program
insert
Code:
void main()
{
char str1[]="Italy";
char str2[2]="Rome";
char *s1=str1,*s2=str2;
while(*s1++ = *s2++);
printf("%s",str1);
}
according to me *s1++ = *s2++ will increment the pointer not the value pointed by it and then it stores the s2++ address inn to it.
But i dont know how the while loop is ending...it is supposed to be an infinite loop since we are assigning s1 and s2 and not comparing any condition ????
I am confused how the str1 points to rome at the end printf statement
Please explain me what really happens at the back ???
Regards,
Ram
-
while(x = y) evaluates the same as while((x = y) != 0), and aside from x getting the value of y, it's the same as while(y != 0). The fact that you are using pointers and autoincrement doesn't change this basic principle.
Code:
char str2[2]="Rome";
Rome is also longer than 2 characters - 5 is the correct number here.
Also note that if you were to use, say, Napoli instead of Rome, you would have to specify a longer space for str1, as it is longer than "Italy".
--
Mats