Question about the order of incrementing i

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dissectcode
    New Member
    • Jul 2008
    • 66

    Question about the order of incrementing i

    Hello - I have this code, and I want to make sure it is incrementing like I am hoping...can someone look at the logic and verify whats happening?

    Code:
    int p[50];
    i = 0;
    
    p[i++] = 'P';
    p[i++] = 20;
    p[i++] = 3;
    p[i]  =  checksumFunct(p[1] , (i - 1) );
    i++;
    so, I want to set the array elements, p[0] = P, p[1] = 20, p[3] = 3, and p[4]=checksum value, and then i is 5. Is this what is happening? Is the 4th element actually p[3]?? Please help...

    btw - I obviously can't increment i and use it in the checksum funciton (illegal b/c sequence points)...
    thanks!
  • Banfa
    Recognized Expert Expert
    • Feb 2006
    • 9067

    #2
    Originally posted by dissectcode
    I want to set the array elements, p[0] = P, p[1] = 20, p[3] = 3, and p[4]=checksum value, and then i is 5.
    Do you realise that in this list you have left out p[2], your indexes go 0, 1, 3, 4?

    If you want want you have written then no because you have missed an increment of i and it's final value is 4. If on the other hand you meant to write

    p[0] = P, p[1] = 20, p[2] = 3, and p[3]=checksum value, and then i is 4

    then the code you have is exactly right.

    Comment

    • dissectcode
      New Member
      • Jul 2008
      • 66

      #3
      Oops you are right - and thank you for the clairity!

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Originally posted by dissectcode
        Oops you are right - and thank you for the clairity!
        Another debugging tip that I have found useful over the years is to use print statements to figure out everything that is going on.

        So in your test, you can print out i after each statement to figure out the value of i before and after those statements (as well as any other variables you might be interested in).

        I usually make a 'debug_printVar s()' function so I only have to code it once, and then just call that function each time I want to see what is going on.

        Comment

        Working...