Thread: array value overwriting another

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    4

    array value overwriting another

    Code:
    #include <stdio.h>
    
    int main(){
    
            int array111[5];
        
            printf("Please enter the 6 array111 numbers.\n");
            scanf("%d %d %d %d %d %d", &array111[0], &array111[1], &array111[2], &array111[3], &array111[4], &array111[5]);
            
        
            
        
            
            
         //set up the values for arr999
        int arr999[6];
        arr999[0] = 0;
        arr999[1] = 0;
        arr999[2] = 0;
        arr999[3] = 10;
        arr999[4] = 100;
        arr999[5] = 10000;
        arr999[6] = 1000000;
        
        printf("array111[0]== %d\n array111[1]== %d\n array111[2]== %d\n array111[3]== %d\n array111[4]== %d\n array111[5]== %d \n", array111[0], array111[1], array111[2], array111[3], array111[4], array111[5]);
    
    }


    Can anyone tell how array111[0] is being overwritten by anything I hardcode into arr999[6]? I can't figure it out

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    The issue is mainly that the arrays are too small for the number of values that you want to hold. The number of elements that any array can have ranges from 0 to N - 1 inclusive, where N is the number of elements. What is actually happening is that array111[6] overflows into array999[0] instead of breaking spectacularly. Since array999[0] happens to be located where array111[6] would be, it worked out for you, but you shouldn't depend on this behavior all the time. It's undefined.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    4
    Quote Originally Posted by whiteflags View Post
    The issue is mainly that the arrays are too small for the number of values that you want to hold. The number of elements that any array can have ranges from 0 to N - 1 inclusive, where N is the number of elements. What is actually happening is that array111[6] overflows into array999[0] instead of breaking spectacularly. Since array999[0] happens to be located where array111[6] would be, it worked out for you, but you shouldn't depend on this behavior all the time. It's undefined.
    Thank you, I misunderstood some important things about arrays. This was fixed by increasing the capacity of each array by 1.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Bizarre array overwriting
    By LanguidLegend in forum C Programming
    Replies: 2
    Last Post: 11-16-2011, 01:19 PM
  2. Overwriting an integer array
    By bridle17 in forum C Programming
    Replies: 2
    Last Post: 10-02-2011, 11:25 PM
  3. How can I avoid overwriting into an array?
    By Pztar in forum C Programming
    Replies: 3
    Last Post: 06-07-2011, 03:17 PM
  4. Overwriting all in array, why?
    By guesst in forum C Programming
    Replies: 7
    Last Post: 10-09-2008, 05:56 PM
  5. Overwriting Array with New Array
    By Osiris990 in forum C++ Programming
    Replies: 14
    Last Post: 02-25-2008, 01:56 PM