Deep Dive
Why Arrays Matter in Programming
Dr. Parashar starts by establishing context: before arrays, if you needed to store 100 student grades, you'd need 100 separate variables—tedious and impractical. Arrays solve this by letting you store multiple values of the same type in a single variable. The key insight is recognizing the limitation of simple variables like `int a = 2`; they hold only one value at a time. If you assign a new value, it overwrites the old one (destructive assignment). Arrays remove this bottleneck. When you declare `int A[5]`, you're telling the compiler: allocate space for 5 integers under one name. All elements must be the same data type—no mixing integers and floats in one array. Dr. Parashar emphasizes this is foundational knowledge because arrays unlock data structures like linked lists and trees that come later.
Declaration and Initialization Syntax
Dr. Parashar walks through the declaration formula: `dataType arrayName[size]`. For example, `int A[5]` means A is an array of integers with maximum capacity 5. The size must be an integer constant—it cannot be a variable in traditional C declarations (she mentions the preprocessor #define as an alternative, but it's outside the current syllabus). If you declare without initializing—`int A[5];`—the size is mandatory; omitting it causes a compiler error. However, if you initialize simultaneously—`int A[5] = {8, 5, 3, 1, 2};`—the size can be omitted because the compiler counts the initializer values. She stresses this distinction repeatedly because exam questions exploit this subtle rule. Array names must follow identifier rules: start with a letter or underscore, use only alphanumeric characters and underscores thereafter.
Memory Allocation and Addressing
When an array is declared, the compiler allocates contiguous memory blocks. Using a 5-element integer array where each integer takes 2 bytes: if A[0] starts at address 100, A[1] occupies 102, A[2] is at 104, A[3] at 106, and A[4] at 108. No gaps exist between elements—they sit back-to-back in memory. The address of the first element (A[0]) is called the base address of the array. Dr. Parashar illustrates this with a diagram showing each element's memory location incrementing by 2. This adjacent storage is fundamental: it's why loops efficiently traverse arrays. Total array size equals number of elements times bytes per element—a 5-element integer array (2 bytes each) = 10 bytes. Understanding this layout is critical for pointer arithmetic, which appears later in the course.
Accessing Elements with Subscripts
Subscripts (also called indices) allow individual element access. In C, subscripts are zero-indexed: valid subscripts for a 5-element array range from 0 to 4. You access element i using `A[i]` notation. A[0] retrieves the first element (8 in the example), A[1] retrieves the second (5), and so on. The subscript determines which position in memory to read or write. Dr. Parashar stresses that you cannot access the entire array by using just the name `A`; you must specify which element via the subscript. Loops make this systematic: `for(i=0; i<=4; i++)` iterates through all valid subscripts. She emphasizes this pattern because most array operations—input, output, calculations—depend on looping and subscripting.
Bound Checking Absence and Practical Implications
A critical C language feature: there is no bounds checking. If your array has indices 0–4 and you try to access A[5] or A[10], the compiler issues no error. At runtime, your program reads or writes to whatever memory lies beyond the allocated block—potentially corrupting other variables or causing undefined behavior. Dr. Parashar demonstrates that modifying the loop condition from `i <= 4` to `i <= 5` compiles without complaint but accesses invalid memory. This is the programmer's responsibility. Unlike languages such as Java, C trusts you to stay within bounds. She repeats this point multiple times because it's a common source of bugs and exam questions. The lesson: always ensure your loop bounds match your array size.