BCA SCHOOL
Programming Tutorials • Notes • Practical • Books
LATEST TUTORIALS

Concept of Array in C: Declaration, Initialization & Indexing | Unit 4 | BKNMU BCA

by BCA School



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

BKNMU Junagadh BCA Sem 1 Unit 4 Derived Data Types Array Indexing Contiguous Memory
In simple words: Storing 100 student marks using 100 individual variables like 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).
📖 The 4 Core Characteristics of an Array

An array data structure is characterized by four foundational properties:

Homogeneous Data

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).

Contiguous Memory

2. Sequential Allocation

Array elements reside side-by-side in uninterrupted, sequential memory addresses without gaps between consecutive blocks.

Zero-Based Index

3. Direct Subscripting

Elements are accessed instantly via indices starting from index 0 up to Size - 1 using the subscript bracket notation [i].

Static Fixed Size

4. Compile-Time Sizing

The total capacity of a standard static array is declared before execution and cannot grow or shrink dynamically at runtime.

1️⃣ Master Table: Array Declaration & Initialization Styles

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.
ℹ️ Total Memory Byte Calculation Formula:
  • Formula: Total Bytes = Array Size × sizeof(Data_Type)
  • Example: For int arr[10]; in Turbo C (where int is 2 bytes): 10 × 2 = 20 Bytes. In modern 64-bit GCC (where int is 4 bytes): 10 × 4 = 40 Bytes.
💻 Practical Code Example: Array Input, Traversal & Sum

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-by-Step Flow: Array Element Addressing & Index Offset
1

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).

2

Step 2: Offset Address Calculation

To reach element marks[i], CPU uses formula: Address = Base_Address + (i × sizeof(data_type)).

3

Step 3: Direct Value Retrieval

Because memory is contiguous, any slot can be accessed in constant time $O(1)$ without searching previous items.

💡 Critical Exam Insight: Array Name is a Constant Pointer: In C, the array identifier without brackets (e.g. marks) acts as a constant pointer holding the base address (&marks[0]). Hence, *(marks + i) is mathematically identical to marks[i].
⚠️ No Array Bound Checking in C: C does not verify array boundaries. If you declare 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.
📝 Quick Revision — Key Exam Points
  • Definition: An array is a collection of homogeneous elements stored at contiguous memory locations.
  • Indexing: Zero-based; valid range is 0 to Size - 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 0 automatically.