Thread: can't understand the increment used here...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    2

    Question can't understand the increment used here...

    whats the reason behind usin' another (i = i + 1) in the 2nd 'for loop'?
    i'm very new at C,and i posted this at another forum but didn't quite get a good enough answer, so pls go easy on me! thanks!!


    Code:
    #include <stdio.h>
    
    main()
    {
    char first[21];
    char sur[21];
    char buffer[42];
    int i, j;
    
    printf("Please enter your first name: ");
    fflush(stdin);
    scanf("%[^\n]", first);
    printf("Please enter your last name: ");
    fflush(stdin);
    scanf("%[^\n]", sur);
    
    /*
    * First, copy the first name into the buffer, a la strcpy()
    
    */
    for (i = 0; first[i] != '\0'; i = i + 1)
    buffer[i] = first[i];
    
    /*
    * Add the space
    */
    buffer[i] = ' ';
    i = i + 1; /* don't forget to adjust the counter */
    
    /*
    * Next, concatentate the surname onto the buffer, a la strcat()
    * Notice that the for loop has TWO increments
    */
    for (j = 0; sur[j] != '\0'; j = j + 1, i = i + 1)
    buffer[i] = sur[j];
    buffer[i] = '\0'; /* don't forget to terminate the string */
    
    printf("The full name is >>%s<<\n", buffer);
    
    fflush(stdin);
    getchar();
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    j is used as the index into the 'sur' array while i is used as the index into the 'buffer' array.
    When 'first' was copied to 'buffer the same index value could be used because first[0] was copied to buffer[0], first[1] to buffer[1] and so on. But when coppying from the 'sur' array the program is adding to the buffer array from the current possition not starting from the start.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > whats the reason behind usin' another (i = i + 1) in the 2nd 'for loop'?
    Well you can try it without, and observe the difference.

    Also, read the FAQ and figure out why fflush(stdin) is bad.
    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.

  4. #4
    ---
    Join Date
    May 2004
    Posts
    1,379
    Every iteration, i is incremented as well as j. Simple as that.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    2

    Thumbs up

    thanks all you guys!!!

    Quantum1024 -i like your answer the most, thats what i couldn't understand before!! thanks a ton!!

    Salem - i tried without it and it didn't print anythin' after the space-char. but i couldn't understand it before...i do now!thanks for the heads-up for fflush(stdin), i'll check it out!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Increment / Decrement Operators - Help
    By shyam168 in forum C Programming
    Replies: 6
    Last Post: 03-29-2006, 09:24 PM
  2. How do I increment printf hex characters?
    By gechno in forum C Programming
    Replies: 2
    Last Post: 05-24-2004, 05:33 PM
  3. increment IP addresses - newbie Q.
    By webwesen in forum C Programming
    Replies: 6
    Last Post: 09-09-2003, 08:25 AM
  4. Post increment and pre increment help
    By noob2c in forum C++ Programming
    Replies: 5
    Last Post: 08-05-2003, 03:03 AM
  5. low value, high value and increment
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 11-25-2001, 09:01 AM