Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Arrays Part 1: Linear Collections

Lesson 16 of 53 in the free Foundation of C & C++ notes on Siksha Sarovar, written by Rohit Jangra.

Storing Data in Bulk

Imagine you need to store the marks of 50 students. Creating 50 variables (mark1, mark2, ...) would be a nightmare. An Array allows you to store all 50 marks under a single name.

Definition of an Array

An array is a collection of items of the same data type stored in contiguous (back-to-back) memory locations.

Declaration and Initialization

int scores[5]; // Reserves space for 5 integers
int ages[3] = {20, 25, 30}; // Initialized with values
int list[] = {1, 2, 3, 4, 5}; // Size is automatically 5

The 0-Index Rule

Array indices in C always start at 0.

  • The first element is at scores[0].
  • The last element of an array of size N is at scores[N-1].
  • Why 0? In C, the index is actually a memory "offset." scores[0] means "0 steps away from the start." This makes the computer's math faster.

Contiguous Memory Visualization

If an int takes 4 bytes and scores[0] is at address 1000:

  • scores[1] is at address 1004
  • scores[2] is at address 1008
  • Because they are in a straight line, the computer can find any element instantly using the formula: Address = Start + (Index * Size).

Common Operations

  1. Traversal: Visiting every element using a for loop.
  2. Searching: Finding if a specific value exists.
  3. Aggregation: Calculating the sum or average of all items.
No Bounds Checking: This is the most dangerous part of C. If you have an array of 5 items and try to access scores[10], C will not stop you. It will just read whatever random junk is at that memory address, which can crash your program or, worse, lead to security hacks like "Buffer Overflow." Always be careful with your indices!