Arrays
Learn how arrays allow you to store multiple values of the same data type under a single name.
Introduction
Imagine you are developing a Student Management System for a school. You need to store the marks of 100 students. One approach is to create 100 separate variables such as marks1, marks2, and so on up to marks100. Managing so many variables becomes difficult, time-consuming, and confusing. Instead, C provides a better solution called an Array. An array allows you to store multiple values of the same data type under a single name.
What is an Array?
An Array is a collection of elements of the same data type stored in contiguous memory locations and accessed using a single name. Instead of creating many variables, you create one array that can store multiple values.
Why Do We Need Arrays?
Without Arrays
With Arrays
Real-World Analogy
Imagine an apartment building. Instead of giving each room a separate building, they all belong to one building. Each room stores different people, but they share the same address. Similarly, an array has one name, while each element has its own position called an index.
Characteristics of Arrays
- Store multiple values
- Store values of the same data type
- Have a fixed size
- Elements are stored in contiguous memory
- Each element is identified using an index
Array Representation & Indexing
Every element in an array has a unique position called an Index. In C, indexing always starts from 0.
Array Name: Marks
Index: 0 1 2 3 4
+----+----+----+----+----+
| 80 | 85 | 90 | 75 | 95 |
+----+----+----+----+----+
• 80 is stored at index 0
• 85 is stored at index 1
• 95 is stored at index 4 (Last element)C begins indexing from zero because the index represents the offset from the first memory location. The first element has an offset of 0, the second has an offset of 1, making array access highly efficient.
Memory Representation
Arrays store elements one after another in memory. This continuous arrangement allows the computer to access any element quickly.
Index: 0 1 2 3 4
+------+------+------+------+------+
| 80 | 85 | 90 | 75 | 95 |
+------+------+------+------+------+
Memory Addresses →Types of Arrays
Arrays are classified based on their dimensions:
1. One-Dimensional Array
Stores elements in a single row. It is the simplest and most commonly used type.
2. Two-Dimensional Array
Stores elements in rows and columns. It resembles a table or matrix.
3. Multi-Dimensional Arrays
Arrays can also have three or more dimensions. These are used in specialized applications such as graphics, scientific computing, image processing, and simulations.
Example: Creating and Accessing an Array
#include <stdio.h>
int main()
{
int marks[5] = {80, 85, 90, 75, 95};
printf("%d", marks[0]);
return 0;
}Line-by-Line Explanation
Line 1: #include <stdio.h>
Includes the Standard Input Output library so that the program can use the printf() function.
Line 3: int main()
The main() function is the starting point of every C program. Execution begins here.
Line 5: int marks[5] = {80, 85, 90, 75, 95};
This statement creates an array named marks. int means it stores integers. [5] means it can store 5 elements. The curly braces contain the initial values.
Line 7: printf("%d", marks[0]);
The printf() function displays the value stored at index 0. marks[0] accesses the first element, which is 80.
Line 9: return 0;
Ends the program successfully and returns control to the operating system.
Program Execution Flow
Advantages of Arrays
Easy Management
One name stores multiple values.
Faster Access
Elements are accessed directly using an index.
Reduced Code
One array replaces many variables.
Better Organization
Related data stays together.
Efficient Processing
Works perfectly with loops.
Real-World Applications
Student System
Store student marks, roll numbers, and attendance.
Banking
Store daily transactions, customer IDs, and balances.
E-Commerce
Store product prices, IDs, and stock quantities.
Hospital
Store patient records, medicine prices, and slots.
Games
Store player scores, levels, inventory, and coordinates.
Limitations of Arrays
- Fixed Size: The size cannot be changed after creation.
- Same Data Type: All elements must belong to the same data type.
- Memory Allocation: Memory for all elements is allocated together, even if some remain unused.
Arrays and Loops
Arrays and loops are often used together. A loop can process every element of an array without writing repetitive code. For example, a loop can display all student marks, calculate the total, find the highest score, or count failed students. This combination makes programs highly efficient.
Common Beginner Mistakes
In C, the first element is always at index 0, not 1.
Trying to access an index outside the array size, such as marks[5] in a size 5 array, leads to undefined behavior. Always ensure the index is within the valid range.
// For an array of size 5:
Size = 5
Indexes = 0, 1, 2, 3, 4
// The last valid index is 4, NOT 5.An array stores only one type of data. Different data types require different arrays or another data structure such as Structures.
Best Practices
- Choose meaningful array names such as studentMarks instead of arr.
- Use the smallest suitable array size to save memory.
- Keep track of valid indexes from 0 to size - 1.
- Process arrays using loops whenever possible.
- Group related data in the same array.
- Avoid accessing elements outside the valid range.
Frequently Asked Questions
What is an array?
An array is a collection of elements of the same data type stored in contiguous memory locations.
Why are arrays used?
Arrays store multiple values using a single name, making programs simpler and more efficient.
What is an index?
An index is the position of an element within an array.
Why does array indexing start from 0?
Because the index represents the offset from the first memory location, and the first element has an offset of zero.
Can an array store different data types?
No. All elements in an array must have the same data type.
Can the size of an array change after creation?
In standard C, no. The size of a regular array is fixed once it is created.
Key Takeaways
- An array stores multiple values of the same data type.
- Elements are stored in contiguous memory locations.
- Every element is identified using an index.
- C arrays always start indexing from 0.
- Arrays improve efficiency by reducing the need for multiple variables.
- Arrays are commonly used with loops to process collections of data.
Summary
Arrays are one of the most important data structures in C programming because they allow programmers to store and manage multiple related values using a single variable name. By organizing data in contiguous memory and accessing elements through indexes, arrays provide fast and efficient data handling. Mastering arrays is essential, as they serve as the foundation for more advanced concepts such as strings, matrices, pointers, and dynamic memory allocation.