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.
A 1D array is the simplest form of an array, often visualized as a single row of data.
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.
int marks[5]; // Declaration: Reserves space for 5 integers int scores[3] = {90, 85, 80}; // Initialization
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.
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.
// A matrix with 2 rows and 3 columns int matrix[2][3] = { {1, 2, 3}, // Row 0 {4, 5, 6} // Row 1 };
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; }
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."
Array Size Calculation: You can find the number of elements in an array using the sizeof operator:
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.
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().
| Feature | 1D Array | 2D Array |
| Visual Shape | A single row/list. | A table/grid. |
| Access | arr[index] | arr[row][col] |
| Looping | Requires one loop. | Requires nested loops. |
| Usage | Linear lists, scores, names. | Matrices, images, game boards. |
Copyright ©2025. All Rights Reserved Emblab THE RAVE INNOVATION