CHAPTER 10
Beginner
Arrays in C
Updated: May 17, 2026
5 min read
# CHAPTER 10
Arrays in C
1. Introduction
Imagine you need to store the grades of 100 students. Creating 100 individual variables (grade1, grade2, ..., grade100) is highly inefficient. An Array is a data structure that stores a fixed-size sequential collection of elements of the *same type*.
2. Learning Objectives
By the end of this chapter, you will be able to:- Declare and initialize arrays.
- Access array elements using indices.
- Traverse arrays using loops.
- Understand how arrays are stored in memory.
- Perform basic searching in an array.
3. Declaring and Initializing Arrays
Declaration Syntax:dataType arrayName[arraySize];
c
Initialization:
c
4. Accessing Array Elements
Arrays are Zero-Indexed. The first element is at index 0.
c
5. Traversing an Array with Loops
To print or process all elements, we use afor loop.
c
6. Passing Arrays to Functions
When you pass an array to a function, you are actually passing its *memory address* (which we will cover fully in pointers). Because of this, you must also pass the size of the array.
c
7. Mini Project: Marks Management System
Let's calculate the total and average marks of a student.
c
8. Memory-Level Explanation
Arrays are stored in contiguous (continuous) memory locations. If anint takes 4 bytes, an array of 3 ints takes 12 contiguous bytes.
Because the memory is contiguous, the CPU can access elements instantly using math: Address = Base_Address + (Index * Data_Size).
9. Common Mistakes
-
Out of Bounds Access: In C, there is no boundary checking. If you declare
int arr[5], and try to accessarr[10], C will blindly read whatever is in that memory location, causing garbage values or a Segmentation Fault (crash).
-
Index starts at 1: Forgetting that arrays start at 0. The last element of
arr[5]isarr[4].
10. Exercises
- 1. Write a program to find the maximum number in an array.
- 2. Write a program that reverses the elements of an array.
- 3. Write a program to search for a specific number in an array (Linear Search) and print its index.
11. MCQ Quiz with Answers
Question 1
What is the index of the first element in an array?
Question 2
How is memory allocated for an array?
Question 3
If int arr[5] = {1, 2};, what is the value of arr[3]?
Question 4
What happens if you access arr[10] on an array of size 5?
Question 5
How do you pass an array to a function?
Question 6
What does sizeof(arr) return if int arr[5]; (assuming 4-byte int)?
Question 8
Which loop is most commonly used to traverse an array?
Question 9
If an array starts at memory address 1000, and stores 4-byte integers, what is the address of arr[2]?
Question 10
How do you find the number of elements in an array dynamically?
12. Interview Questions
- Q: Why do arrays in C start at index 0?
- Q: How is an array passed to a function (pass by value or reference)? Explain why.
- Q: What is the formula to calculate the memory address of the i-th element of a 1D array?
13. Summary
Arrays store multiple elements of the same type in contiguous memory. They are zero-indexed, meaning traversal loops usually go from0 to size - 1. Arrays are efficient for accessing data but lack built-in bounds checking, requiring the programmer to manage bounds carefully.