Concept of Array in C: Declaration, Initialization & Indexing | Unit 4 | BKNMU BCA
BKNMU Junagadh | BCA Sem 1 | Unit 4
Concept of Array in C: Declaration, Initialization & Memory Layout
Complete Guide to Homogeneous Data Structures, Zero-Based Indexing, Contiguous Memory, and Bound Safety
m1, m2, m3... is inefficient and unmanageable. An Array solves this problem by grouping multiple items of the same data type under a single variable name in sequential, contiguous memory locations. This complete fundamental guide is structured specifically for BKNMU BCA Semester 1 (Unit 4).
An array data structure is characterized by four foundational properties:
1. Fixed Data Type
All elements within a single array must share the exact same data type (e.g., all int, all float, or all char).
2. Sequential Allocation
Array elements reside side-by-side in uninterrupted, sequential memory addresses without gaps between consecutive blocks.
3. Direct Subscripting
Elements are accessed instantly via indices starting from index 0 up to Size - 1 using the subscript bracket notation [i].
4. Compile-Time Sizing
The total capacity of a standard static array is declared before execution and cannot grow or shrink dynamically at runtime.
A complete reference of syntax patterns, memory outcomes, and initialization behaviors:
| Declaration Pattern | Example Syntax | Resulting Memory & Value State |
|---|---|---|
| Uninitialized Declaration | int marks[5]; |
Allocates 5 integer blocks containing unpredictable garbage values. |
| Full Initialization | int arr[3] = {10, 20, 30}; |
Allocates 3 blocks and fills all slots explicitly in order. |
| Partial Initialization | int arr[5] = {10, 20}; |
Initializes arr[0]=10, arr[1]=20; remaining 3 slots default automatically to 0. |
| Unsized Initialization | int arr[] = {1, 2, 3, 4}; |
Compiler automatically calculates the size as 4 elements based on provided items. |
- Formula:
Total Bytes = Array Size × sizeof(Data_Type) - Example: For
int arr[10];in Turbo C (whereintis 2 bytes):10 × 2 = 20 Bytes. In modern 64-bit GCC (whereintis 4 bytes):10 × 4 = 40 Bytes.
The following program demonstrates reading values into an array using loops, traversing elements, and calculating total score:
#include <stdio.h>
int main() {
int marks[5];
int i, total = 0;
// 1. Reading 5 elements from user
printf("Enter 5 subject marks:\n");
for (i = 0; i < 5; i++) {
printf("Mark for subject [%d]: ", i);
scanf("%d", &marks[i]); // Passing address of each array slot
}
// 2. Processing elements (Accumulating sum)
for (i = 0; i < 5; i++) {
total += marks[i];
}
// 3. Displaying stored values & sum
printf("\n--- Marks Summary ---\n");
for (i = 0; i < 5; i++) {
printf("marks[%d] = %d (Address: %p)\n", i, marks[i], &marks[i]);
}
printf("Total Sum = %d\n", total);
return 0;
}
Step 1: Base Address Allocation
The array name represents the base memory address of index 0 (e.g. marks points to &marks[0] at address 1000).
Step 2: Offset Address Calculation
To reach element marks[i], CPU uses formula: Address = Base_Address + (i × sizeof(data_type)).
Step 3: Direct Value Retrieval
Because memory is contiguous, any slot can be accessed in constant time $O(1)$ without searching previous items.
marks) acts as a constant pointer holding the base address (&marks[0]). Hence, *(marks + i) is mathematically identical to marks[i].
int a[5] and attempt to write to a[10], the compiler will not throw an error; it will corrupt neighboring memory causing segmentation faults or unexpected bugs.
- Definition: An array is a collection of homogeneous elements stored at contiguous memory locations.
- Indexing: Zero-based; valid range is
0toSize - 1. - Array Type: Classified as a derived data type in C.
- Base Address: The memory location of the first element (
index 0). - Partial Initialization: Unspecified elements are initialized to
0automatically.