Results 1 to 3 of 3

Thread: Code Output

  1. #1
    Junior Member
    Join Date
    Jan 2008
    Answers
    3

    Code Output

    What the following code is doing?

    char *a=...., *b=....;
    while(*b++ = *a++);


    Guys- Please help me solve this problem. Thanks a lot.


  2. #2

    Re: Code Output

    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


  3. #3
    Contributing Member
    Join Date
    Dec 2007
    Answers
    46

    Re: Code Output

    Quote Originally Posted by heidi012979 View Post
    What the following code is doing?

    char *a=...., *b=....;
    while(*b++ = *a++);


    Guys- Please help me solve this problem. Thanks a lot.
    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.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
About us
Applying for a job can be a stressful and frustrating experience, especially for someone who has never done it before. Considering that you are competing for the position with a at least a dozen other applicants, it is imperative that you thoroughly prepare for the job interview, in order to stand a good chance of getting hired. That's where GeekInterview can help.
Interact