Special Offer Diwali Special Bundle Offer: 3 courses for ₹8,999, save ₹9,001 (50% OFF)! Limited seats available. View Bundles →
0% Completed
Tutorials / C Programming Tutorial / Chapter 3: Functions, Arrays & Pointers
C ⏱ 12 min read

Arrays: 1D and 2D Arrays

An array is a contiguous block of memory storing multiple elements of the exact same data type.

What is it?

A fixed-size sequential collection of elements of identical type allocated continuously in RAM.

Why is it used?

Stores lists of numbers, scores, matrices, images (grids of pixels), and sensor sample buffers.

Detailed Explanation

Array Characteristics: • Contiguous Allocation: Elements sit directly next to each other in RAM (e.g. if `arr[0]` is at address 1000, `arr[1]` is at 1004 for 4-byte integers). • Zero-based indexing: Indices run from `0` to `size - 1`. • Boundary Checking: C does NOT perform array bounds checking! Accessing `arr[10]` on a 5-element array reads arbitrary garbage memory or causes segmentation faults. • 2D Arrays: Grids declared as `matrix[rows][cols]` stored in row-major order.

Syntax Structure

Syntax Blueprint
int numbers[5] = {10, 20, 30, 40, 50};
int matrix[2][3] = { {1, 2, 3}, {4, 5, 6} };

Code Example

C Code
#include <stdio.h>

int main() {
    // 1D Array of Course Scores
    int scores[5] = {88, 92, 79, 95, 84};
    int count = sizeof(scores) / sizeof(scores[0]);
    int sum = 0;
    
    printf("=== 1D Array: Student Scores ===\n");
    for (int i = 0; i < count; i++) {
        printf("Student #%d: %d\n", i + 1, scores[i]);
        sum += scores[i];
    }
    printf("Class Average: %.1f\n", (float)sum / count);
    
    // 2D Array: 2x3 Matrix
    int grid[2][3] = {
        {10, 20, 30},
        {40, 50, 60}
    };
    
    printf("\n=== 2D Array: Matrix Grid ===\n");
    for (int row = 0; row < 2; row++) {
        for (int col = 0; col < 3; col++) {
            printf("%4d", grid[row][col]);
        }
        printf("\n");
    }
    
    return 0;
}

Explanation of the Code

• `sizeof(scores) / sizeof(scores[0])` calculates array length dynamically at compile time. • Nested `for` loops traverse rows and columns of the 2D grid.

Expected Output

=== 1D Array: Student Scores === Student #1: 88 Student #2: 92 Student #3: 79 Student #4: 95 Student #5: 84 Class Average: 87.6 === 2D Array: Matrix Grid === 10 20 30 40 50 60

Real-World Application

Used across computer vision and 3D graphics (matrix transformations) to store pixel grids and vertices.

Common Mistakes to Avoid

• Accessing out-of-bounds indices (C will not warn you, creating dangerous buffer overflow security vulnerabilities). • Forgetting that arrays cannot be resized once declared (use dynamic memory allocation for resizable arrays).

Quick Practice Challenge

Find the maximum element in an array of 5 integers using a single `for` loop.

Key Takeaways

• Arrays store homogeneous elements contiguously in RAM. • Calculate size with `sizeof(arr) / sizeof(arr[0])`. • Beware: C does not check array bounds at runtime.