Array size

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mohanphani2003
    New Member
    • Apr 2009
    • 8

    Array size

    Hi all,

    What will happen with this code.

    #include<stsio. h>
    void main( )
    {
    int arr[3];
    int i;
    int j=3;
    for (i=0;i<=3;i++);
    {
    j=j+1;
    arr[i]=j;
    }
    }

    what will be the contents on the array?
  • mohanphani2003
    New Member
    • Apr 2009
    • 8

    #2
    Can any one help me pls...

    Comment

    • donbock
      Recognized Expert Top Contributor
      • Mar 2008
      • 2427

      #3
      1. Change "stsio.h" to "stdio.h".
      2. Add "printf("ar r[%d] = %d\n", i, arr[i]);" immediately after the line that assigns a value to arr[i].
      3. Use CODE tags when you post code snippets on this bulletin board.

      Comment

      • mohanphani2003
        New Member
        • Apr 2009
        • 8

        #4
        Thanks your suggestion is very helpful for me.

        Could you pls clarify this.

        Will it stores the default values or else.

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          Originally posted by mohanphani2003
          Will it stores the default values or else.
          I don't see any default values in your program. You will have to clarify what you mean.

          1. Variable 'arr' is defined as an automatic variable within function 'main'. Without an initializer, the initial value of an automatic variable is unpredictable; and can be expected to be different each time you run the program.

          2. Had you defined 'arr' outside of function scope, then the C Standard requires it to be initialized to all zeroes before your program was started. (But you didn't do this, so this rule doesn't apply to your program.)

          3. Within your loop you assign a value to successive elements of array 'arr'. This is the only place I see where values are written into 'arr'.

          By the way ... This is the Insights bulletin board. You should't post questions here. Questions should go in the answers bulletin board.

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Also your code writes 1 more entry into array than it actually.

            Comment

            • mohanphani2003
              New Member
              • Apr 2009
              • 8

              #7
              So what will happen?

              Will that extra value be stored in array or not?

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                You will get undefined behaviour by writing outside the memory bounds of the array. Anything may happen.

                You should avoid undefined behaviour at all costs, in the case of arrays it is simple, if the array is declared with a size x then the valid range for accesses to that array is 0 to x-1.

                Comment

                • mohanphani2003
                  New Member
                  • Apr 2009
                  • 8

                  #9
                  Thank you.

                  Your reply is very help ful for me.

                  Comment

                  • JosAH
                    Recognized Expert MVP
                    • Mar 2007
                    • 11453

                    #10
                    Originally posted by mohanphani2003
                    Hi all,

                    What will happen with this code.

                    #include<stsio. h>
                    void main( )
                    {
                    int arr[3];
                    int i;
                    int j=3;
                    for (i=0;i<=3;i++);
                    {
                    j=j+1;
                    arr[i]=j;
                    }
                    }

                    what will be the contents on the array?
                    1) most likely the file stsio.h doesn't exist so compilation will fail;
                    2) it's 'int main()', not 'void main()'; a pedantic compiler may object;
                    3) that trailing ';' after the for loop header is intentional?
                    4) you are writing outside of an array: that's undefined behaviour;

                    Was that crappy code given to you as your homework?

                    kind regards,

                    Jos

                    Comment

                    • mohanphani2003
                      New Member
                      • Apr 2009
                      • 8

                      #11
                      It's a typing mistake.Pls ignore these mistakes.I am sorry for this.

                      Comment

                      Working...