Something in the site's tutorial i can't understand in the array part.

This is a discussion on Something in the site's tutorial i can't understand in the array part. within the C Programming forums, part of the General Programming Boards category; in the array lesson in the site's tutorial this code is goven and im not convinced with it because the ...

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    46

    Something in the site's tutorial i can't understand in the array part.

    in the array lesson in the site's tutorial this code is goven and im not convinced with it because the 'i' is incremented before its use since the ++ comes before it.and as i know that this wont excute the first element in the string array.am i right?
    Code:
    char astring[10];
    int i = 0;
    /* Using scanf isn't really the best way to do this; we'll talk about that 
       in the next tutorial, on strings */
    scanf( "%s", astring );
    for ( i = 0; i < 10; ++i )
    {
        if ( astring[i] == 'a' )
        {
            printf( "You entered an a!\n" );
        }
    }

  2. #2
    @codeguru
    Join Date
    Jan 2006
    Posts
    34
    no ^.^ i dont think so...

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    46
    explanation?

  4. #4
    @codeguru
    Join Date
    Jan 2006
    Posts
    34
    the compiler builds the for loop like this:

    Code:
    /*/for ( i = 0; i < 10; ++i )
    {
        if ( astring[i] == 'a' )
        {
            printf( "You entered an a!\n" );
        }
    }/*/
    
    i = 0
    while (i < 10)
    {
        if ( astring[i] == 'a' )
        {
            printf( "You entered an a!\n" );
        }
        ++i;
    }

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    It's a for loop, i is incrimented at the end of each pass thru the loop, in this case it dosen't matter weather you use i++ or ++i.
    If you were doing something like this
    Code:
    a=++i;
    Then i would be incrimented before being assigned to a.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 08:25 PM
  2. 1-D array
    By jack999 in forum C++ Programming
    Replies: 24
    Last Post: 05-12-2006, 08:01 PM
  3. Replies: 5
    Last Post: 01-25-2006, 09:53 PM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. mode of an array
    By Need Help in forum C Programming
    Replies: 15
    Last Post: 09-17-2001, 09:03 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21