Thread: New statement of loop for.

  1. #1
    Banned
    Join Date
    Apr 2015
    Posts
    596

    New statement of loop for.

    Hi,
    I've encountered while surfing in the websites a c code and seemed weird to me,
    what confusing me; is the statement of its loop for, what does it stand for in this case? he used pointers in it also , how it comes?
    Code:
    int strcmp(char * left, char * right)
    {
     for ( ; *left && *right; left++, right++ )
        if ( *left != *right ) break;
    }
    Any help for illustrating what for statement means in this case?



    thanks.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    What is it that you don't understand, could you be a little more specific?
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Consider the "for()" loop:

    Code:
    for( /* initialization */ ; /* condition */ ; /* update */)
    {
        // ...
    }
    Technically, each of those parameters is optional (though the semi-colons are mandatory).

    For instance, one way to write an infinite loop is by leaving all of those blank:

    Code:
    for( ; ; )  // infinite loop
    {
        // ...
    }
    In the example you provided, initialization is not required - so that spot is left empty.

    The arguments to the function are char pointers (presumably strings). So the "if()" deferences the pointers to see what characters are contained at the given addresses.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The function is missing a return statement. I suppose you could do:
    Code:
    return *left < *right ? -1 : *left > *right ? +1 : 0;
    Does this help you in any way?
    Last edited by whiteflags; 12-21-2015 at 04:55 PM.

  5. #5
    Banned
    Join Date
    Apr 2015
    Posts
    596
    Quote Originally Posted by whiteflags View Post
    The function is missing a return statement. I suppose you could do:
    Code:
    return *left < *right ? -1 : *left > *right ? +1 : 0;
    Does this help you in any way?
    ye, appreciated!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 03-13-2014, 12:01 AM
  2. A for-loop as a while statement?
    By c-da in forum C Programming
    Replies: 3
    Last Post: 11-09-2012, 08:13 AM
  3. Using while loop for if statement.
    By Galens in forum C++ Programming
    Replies: 5
    Last Post: 10-03-2008, 03:14 PM
  4. if statement in while loop
    By C++Noobie in forum C++ Programming
    Replies: 4
    Last Post: 11-10-2006, 11:38 AM
  5. For/loop statement
    By stevedawg85 in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2006, 08:03 PM