C

Arrays in C Programming

An array is a fixed-size, sequential collection of elements of the same data type. Instead of declaring individual variables (like score1, score2, score3), you declare one array variable (like scores) and use index numbers to access each element.

In C, arrays are stored in contiguous memory locations, meaning each element sits right next to the other in the computer's RAM.

1. One-Dimensional (1D) Arrays

A 1D array is the simplest form of an array, often visualized as a single row of data.

The Theory: Indexing and Memory

  • Zero-Based Indexing: In C, the first element of an array is always at index 0. If an array has $N$ elements, the last index is $N-1$.

  • Static Size: The size of an array must be known at compile time. Once defined, it cannot be changed.

  • Contiguous Storage: If an integer array starts at memory address $1000$ and an int is 4 bytes, the next element will be at $1004$, the next at $1008$, and so on.

Syntax & Initialization:


int marks[5]; // Declaration: Reserves space for 5 integers
int scores[3] = {90, 85, 80}; // Initialization

2. Multi-Dimensional Arrays

Multi-dimensional arrays are "arrays of arrays." The most common form is the Two-Dimensional (2D) array, which can be visualized as a table with rows and columns.

The Theory: Rows and Columns

A 2D array is declared using two sets of square brackets. The first represents the rows and the second represents the columns.

  • Conceptual View: A $3 \times 4$ array is a grid with 3 rows and 4 columns.

  • Physical Memory View: Even though we think of it as a grid, the computer still stores it as one long continuous line of memory. C uses Row-Major Order, meaning it stores all elements of the first row, then all elements of the second row, etc.

Syntax & Initialization:

// A matrix with 2 rows and 3 columns
int matrix[2][3] = {
    {1, 2, 3}, // Row 0
    {4, 5, 6}  // Row 1
};

3. Comprehensive Code Example

This program demonstrates how to store data in a 1D array and how to process a 2D matrix.


#include <stdio.h>

int main() {
    // --- 1D Array Example ---
    int numbers[5] = {10, 20, 30, 40, 50};
    int sum = 0;

    for(int i = 0; i < 5; i++) {
        sum += numbers[i];
    }
    printf("Sum of 1D array elements: %d\n", sum);

    // --- 2D Array Example ---
    int matrix[2][2];
    
    // Taking input for a 2x2 matrix
    printf("\nEnter 4 elements for a 2x2 matrix:\n");
    for(int i = 0; i < 2; i++) {
        for(int j = 0; j < 2; j++) {
            scanf("%d", &matrix[i][j]);
        }
    }

    // Displaying the matrix
    printf("The 2x2 Matrix is:\n");
    for(int i = 0; i < 2; i++) {
        for(int j = 0; j < 2; j++) {
            printf("%d\t", matrix[i][j]);
        }
        printf("\n"); // New line after each row
    }

    return 0;
}

4. Critical Rules and Pitfalls

  1. Array Bounds Checking: C does not check if an index is out of bounds. If you have int arr[5] and try to access arr[10], the program will not show an error during compilation; it will simply access a random memory location, leading to a "Segmentation Fault" or "Garbage Values."

  2. Array Size Calculation: You can find the number of elements in an array using the sizeof operator:

    $$Number\ of\ elements = \frac{sizeof(array)}{sizeof(array[0])}$$
  3. Passing Arrays to Functions: When you pass an array to a function, it is always passed by reference (via its memory address). This means any changes the function makes to the array will affect the original array.

  4. No Direct Copying: You cannot copy one array to another using =, like arr1 = arr2;. You must copy each element one by one using a loop or a function like memcpy().

5. Summary Table

Feature1D Array2D Array
Visual ShapeA single row/list.A table/grid.
Accessarr[index]arr[row][col]
LoopingRequires one loop.Requires nested loops.
UsageLinear lists, scores, names.Matrices, images, game boards.
Upcoming Course
Upcoming Course
Learn More
Instructor Tips
Instructor Tips
View Tips
Join Community
Join Community
Join Now