What the following code is doing?
char *a=...., *b=....;
while(*b++ = *a++);
Guys- Please help me solve this problem. Thanks a lot.
What the following code is doing?
char *a=...., *b=....;
while(*b++ = *a++);
Guys- Please help me solve this problem. Thanks a lot.
Make ur question more elaborate.
I suppose like
Char *a="hello"; char *b="world'' This became read only string
Then while doing operation it will give err.
Becaz of writing at read only place
The expression inside the while loop is: *b++ = *a++.
Let us simplify this expression into two steps:
- *b = *a
- b++; a++
Since b and a are pointing to strings as per your email,
- *b = *a will copy one character from a to b.
- b++ and a++ will increment the pointer to the next address in the character arrays and b.
- Due to the while loop, we keep incrementing the pointers and copy all the characters from array a to array b.
- When will the while loop break?? It will when the expression inside the while loop evaluates to false. This will happen when either *b or *a will be equal to zero. When will that happen?? When we hit the NULL termination for either array b or array y.
So, what we have here is a fancy way of copying character array a to array b.
Hope that helps.