Thread: A while problem

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    26

    A while problem

    This code copies the contents of an array of characters (from) to another array(to)
    my code was
    Code:
    void cpy_string(char *to,char *from)
    {
    printf("Copying ...\n");
    while(*from!='\0')
         *to++=*from++;
    }
    But the solution of the book i'm studying from was
    Code:
    void cpy_string(char *to,char *from)
    {
    	printf("Copying...\n");
    	while(*to++ = *from++);
    }
    now i don't get the next part of code

    Code:
    while(*to++ = *from++);
    can any one explain please ?!

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Code:
       while (*to++ = *from++);
    expands to (by moving the post-increments out of the while statement into the body)
    Code:
       while (*to = *from)
       {
           ++to;
           ++from;
       }
    and since (a = b) sets a to be equal to b, and gives a result of b, this is equivalent to
    Code:
        while (*from)
        {
             *to = *from;
             ++to;
             ++from;
        }
    which, if you want to make the three statements into one, can be done as;
    Code:
        while (*from)
        {
             *to++ = *from++;
        }
    and the braces can be removed .... to get
    Code:
        while (*from) *to++ = *from++;
    Now, since "while (x)" is equivalent to "while (x != 0)", this is equivalent to your code.

    So all you've done is show there is more than one way to solve this problem.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    26
    when the codition
    Code:
    while(*from)
    become false ??
    i think when *from is equal to 0 .. right ?
    so the loop will keep working cause we don't know if there is zero in the array or not

    and how
    [code]
    while (*from) *to++=*from++;
    [/code/
    is equal to
    Code:
    while(*to++ = *from++);
    please explain and be patient with me.. i'm still a beginner ..
    Thanks .

  4. #4
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by Salahuddin View Post
    when the codition
    Code:
    while(*from)
    become false ??
    i think when *from is equal to 0 .. right ?
    Yes, these are all equivalent:
    while(*from)
    while(*from!=0)
    while(*from!='\0')
    so the loop will keep working cause we don't know if there is zero in the array or not
    If you can't guarantee that there will be a terminating zero then you have to think of some other way to stop the loop, like passing a length argument to the function.
    But since the function is named cpy_string I think it's safe to assume that the user should understand to only pass in valid strings.
    and how
    Code:
    while (*from) *to++=*from++;
    is equal to
    Code:
    while(*to++ = *from++);
    please explain and be patient with me.. i'm still a beginner ..
    Thanks .
    They are not equivalent. The first one (and your function) does not copy the string terminator, but the second one does.

    As for how it works:
    Code:
    while(*to++ = *from++);
    1) *to = *from
    2) test if *to is true
    3) to++; from++;
    4) if (2) was true then execute the loop body, which is empty. Just a single ;. And then go back to (1)
    5) if (2) was not true then exit the loop

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    26
    Thanks _mike ... really you are great .. it's so clear for me now .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp problem, whats the problem, i cant figure it out!
    By AvaGodess in forum C Programming
    Replies: 14
    Last Post: 10-18-2008, 06:45 PM
  2. Replies: 4
    Last Post: 10-16-2008, 07:30 PM
  3. sturct/pointer problem, and fscanf problem
    By hiphop4reel in forum C Programming
    Replies: 6
    Last Post: 07-28-2008, 09:40 AM
  4. Replies: 27
    Last Post: 10-11-2006, 04:27 AM
  5. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM