Array

This is a discussion on Array within the C Programming forums, part of the General Programming Boards category; Hi all, I'm a beginner in C and was trying my hands at some coding with arrays. I had the ...

  1. #1
    Registered User
    Join Date
    Dec 2006
    Posts
    1

    Arrow Array

    Hi all, I'm a beginner in C and was trying my hands at some coding with arrays. I had the array initialized with a size of ten, but later found that I could assign values to array elements that is beyond the 9th element. Why isn't there any exceptions thrown? Is this normal in C? If this is the case then why should we specify the array size in the first place?

    Here's a snippet of my code:

    Code:
    #define MAX 10
    
    int a[MAX];
    
    int i;
    	
    for ( i=0; i<MAX; i++) {
          a[i] = /*some random number*/;
          printf("%d\n", a[i]);
    }
    
    a[20] = 2020;
    printf("%d", a[20]);  // prints 2020, no exceptions!
    Thanks in advance!

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    C does not check bounds - it's your job...
    when you access memory out of bounds - the result is undefined, can be a GPF, can be some over variable overwritten, or you can not note it till it runs in some other environment
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. Replies: 6
    Last Post: 11-09-2006, 02:28 AM
  3. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 07:31 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21