Array negative indexing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kmon
    New Member
    • Mar 2011
    • 3

    Array negative indexing

    Code:
    int a[3][3]={1,2,3,4,5,6,7,8,9};
    int j,i,s=10;
    for(i=0;i<3;i++) s=s-a[i][2-i]; printf("%d\n", s);
    for(j=0;j<3;j++) s=s-a[i][2-i]; printf("%d", s);

    This is piece of C code, please some one can give the output

    and pls EXPLAIN it ?

    If possible, try to send a mail to <email_remove d>

    Thank you
    Last edited by Dormilich; Mar 21 '11, 10:19 AM. Reason: please use [CODE] [/CODE] tags when posting code, removed email (anti spam measure)
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    try printing the values of the array elemements indexed, e.g.
    Code:
    int a[3][3]={1,2,3,4,5,6,7,8,9};
    int j,i,s=10;
    for(i=0;i<3;i++) printf("%d ", a[i][2-i]);
    printf("\n");
    for(j=0;j<3;j++) printf("%d ", a[i][2-i]);
    printf("\n");
    that should help you understand what is happening

    in the following the for loop control variable is j but the array elements are indexed using i, should it be j ?
    Code:
    for(j=0;j<3;j++) s=s-a[i][2-i]; printf("%d", s);
    also
    Code:
    int a[3][3]={1,2,3,4,5,6,7,8,9};
    a is a 2D so should be initialised as such. the gcc compiler gives the following warning
    Code:
    C:\temp\zz.c|3|warning: missing braces around initializer|

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      Each loop executes only three times, so it is pretty easy to unroll them. Line 4 is certainly a typo, but I won't try to guess what it should be.
      Code:
      s = 10;    // line 2
      s -= a[0][2];   // line 3, i=0
      s -= a[1][1];   // line 3, i=1
      s -= a[2][0]    // line 3, i=2
      printf("%d\n", s);  // line 3
      s -= a[3][-1];  // line 4, j=0
      s -= a[3][-1];  // line 4, j=1
      s -= a[3][-1];  // line 4, j=2
      printf("%d\n", s);  // line 4
      The behavior is undefined when you access past the bounds of an array; such as a[3][-1], where both indices are out-of-range. The compiler may give you a nice informative error message for undefined behavior, but the C Standard does not require that of it.

      OK, I'll make a guess at what line 4 should have been. Line 3 traverses the right-to-left diagonal. Perhaps line 4 is meant to traverse the other diagonal.

      Comment

      • kmon
        New Member
        • Mar 2011
        • 3

        #4
        Hi
        when i tried this code (I used Dev C++) (outputs are given)
        Code:
        int a[3][3]={1,2,3,4,5,6,7,8,9};
                 int j,i,s=5;
                 for(i=0;i<3;i++) 
                        s=s-a[i][2-i];
                 printf("%d\n",s); /* o/p is -10  */
        
                 printf("a[%d][%d] is %d\n",2-i,i,a[2-i][i]);
                                   /* o/p is a[-1][3] is 1   */
                 printf("a[%d][%d] is %d\n",i,2-i,a[i][2-i]);
                                  /* o/p is a[3][-1] is 9  */
                 printf("a[%d][%d] is %d\n",2-i,i,a[2-i][i]);
                                  /* o/p is a[-1][3] is 1  */
                 printf("a[%d][%d] is %d\n",1-i,i,a[1-i][i]);
                                 /* o/p is a[-2][3] is 10  */
                 printf("a[-1][-1] is %d\n",a[-1][-1]);
                        /* o/p is some RANDOM VALUE each time*/
                 printf("a[3][3] is %d\n",a[3][3]);
                                 /* o/p is 2686792 */
        I'm confused with this, since one cannot give array index out of the boundary....

        Pls some one explain it...
        thank u

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          No one has said that you can't give out-of-bounds array indexes, only that if you do you get undefined behaviour so it should be avoided. That is it is up to you as the programmer to make sure it doesn't happen, the compiler/computer does check for you.

          Undefined behaviour is bad because the computer can literally do anything when this behaviour is invoked, including but not limited to
          • Work in what appears to be a sensible or correct fashion
          • Crash
          • Corrupt program data
          • Corrupt the hard disk
          • Make demons fly out of your nose


          It is not constrained in anyway.

          When you ran it you got the first item on that list but you have no guarantee that the same program run again will do the same thing.

          Avoid undefined behaviour which means, among several other things, avoid access arrays outside their boundaries.

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            Did you write this code yourself or are you trying to understand code written by somebody else?
            Do have any reason to believe the code is correct?

            Comment

            • kmon
              New Member
              • Mar 2011
              • 3

              #7
              I got it from one of my friend, and myself checked it with Dev C++. (O/P is given in the code itself)

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                It doesn't matter what the output is, the behaviour is undefined, that means you can not rely on what is happening always happening.

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  The program is incorrect ... even if it happens to produce the desired output. The reason it is incorrect is that undefined behavior cannot be relied upon to be consistent. Perhaps you get the desired output today, but tomorrow you might get dramatically different output for the same inputs.

                  Comment

                  Working...