Thread: I don't understand this for function.

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    7

    I don't understand this for function.

    WHEN YOU TYPE

    Code:
    void func(char *s1, char *s2, char *s3){
     while(*s1 != '\0' ) s1++;
     while(*s2 != '\0' ) s2++;
     for(; *s1=*s2=*s3; s1++, s2++, s3++);
    }
    
    main() {
     char s1[10] = "ABC", s2[10]="DEF";
     char s3[10] = "GHI";
     func(s1,s2,s3);
     printf("%s, %s %s", s1, s2, s3);
     }
    this code gives ABCGHI, DEFGHI GHI as the result....
    how come there is a command assignment in the middle of for function
    and why is the result like that? I would be very much appreciated if
    you tell me step by step
    Last edited by Dorky King; 06-12-2007 at 02:58 PM. Reason: typed it wrong

  2. #2
    Registered User
    Join Date
    May 2007
    Posts
    7
    come on experts!!! I'm desperate...

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    This
    Code:
    for(initialization; condition; increment) { /* ... */ }
    is the same as
    Code:
    initialization;
    while(condition) {
        /* ... */
        increment;
    }
    or
    Code:
    initialization;
    value = condition;
    while(value) {
        /* ... */
        increment;
        value = condition;
    }
    It follows that this
    Code:
    for(; *s1=*s2=*s3; s1++, s2++, s3++);
    is the same as
    Code:
    while(*s1=*s2=*s3) {
        s1++, s2++, s3++;
    }
    or
    Code:
    value = *s1=*s2=*s3;
    while(value) {
        s1 ++;
        s2 ++;
        s3 ++;
        value = *s1=*s2=*s3;
    }
    Oh, and this
    Code:
    while(*s1 != '\0' ) s1++;
    is the same as
    Code:
    value = (*s1 != '\0');
    while(value) {
        s1 ++;
        value = (*s1 != '\0');
    }
    Does that help answer your question?

    [edit] Hint: loops like this
    Code:
    while(*s1 != '\0' ) s1++;
    just increment s1 until s1 is pointing at the NULL terminator. Thus, after the first two lines of func(), s1 and s2 have both been incremented to point at their NULL terminators. [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    You post again after 4 minutes? Don't be lame. This isn't a drive-through fast food service, and even those might take longer than 4 minutes.

    You posted a function that must be related to a programming course or something else designed to teach you, because it would be overly difficult to read otherwise.

    As with the answer, I compiled it myself and got this:

    Code:
    ABCGHI, DEFGHI GHI
    Inside func, basically s1 and s2 will point to the end of the strings they respectively point to.

    When the for loop occurs, this is what you have:

    Code:
             s1
              |
              |
              v
    A, B, C, \0
    
    
             s2
              |
              |
              v
    D, E, F, \0
    
    s3
    |
    |
    v
    G, H, I, \0
    From there on, the condition of the for loop performs a copy. This is dangerous to do. In this program, it's guarenteed to work (I think), but the function assumes it's being passed large enough buffers, which can result in problems (similar to gets()).

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    7
    opps.... the result is ABCGHI, DEFGHI GHI... I'm sorry mistake

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >this code gives ABCGHI, DEFGHI DEF as the result....
    I suggest a new compiler.

  7. #7
    Registered User
    Join Date
    May 2007
    Posts
    7
    you guys are awesome... thank you so much!!!!

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Very strange, but my output is
    "ABCGHI, DEFGHI GHI"

    I think - you miswrited it...

    also main should be
    int main(void)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User
    Join Date
    May 2007
    Posts
    7
    Now I've got another problem...
    at the end of for statement, pointer s1, s2, s3are all pointing to the '\0' character of each
    string.
    when you come back to main and print each string why does it print the whole thing? is it supposed to???
    and oh, another one.
    When does the for statement officially end??
    how does it get "false" value to end the statement???

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    There's no answer to that level of impatience.

    Oh wait, there is.
    http://catb.org/~esr/faqs/smart-questions.html#urgent
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  11. #11
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I answered these new questions in the other topic....

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >When does the for statement officially end??
    At the semicolon.
    >how does it get "false" value to end the statement???
    A false is a zero, so when the value of *s3 becomes zero (the string terminator is found), then *s2 will be assigned to 0, as will *s1, so the expression evaluates to 0, or false.

    I'm not sure I can do a good job of explaining question 1, so I'll defer.

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    for ends when condition is false

    This *s1=*s2=*s3 is false when the return value is 0, the return value is the value assigned to *s1. So when the null-character from the string s3 is copied to s2 ans s1 - for loop ends

    As to the first questing - parameters in C are passed by value, so original pointers are not changed, func function changes only the local copies of the pointers s1,s2,s3
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Which ffunction do I use?
    By Matt13 in forum C Programming
    Replies: 2
    Last Post: 06-21-2004, 08:37 AM