Unit 6: Arrays
Table of Contents
1. What are Arrays?
An Array is a fixed-size, sequenced collection of elements of the same data type. It is used to store multiple values in a single variable, rather than declaring separate variables for each value.
Key Concept: Arrays are used when we need to handle a large amount of related data of the same type (e.g., marks of 50 students).
- Homogeneous: All elements must be of the same type (all int, all float, etc.).
- Contiguous Memory: Elements are stored in adjacent memory locations.
- Index-Based: Each element is accessed using a unique index number, starting from 0.
2. Creating and Declaring Arrays
To declare an array in C, you specify the data type of the elements and the number of elements required by the array.
Syntax:
data_type array_name[array_size];
Example: int marks[5];
This tells the compiler to reserve space for 5 integers under the name 'marks'.
3. Initialization of Arrays
Initialization refers to assigning values to the array elements at the time of declaration.
- Static Initialization:
int age[5] = {20, 22, 19, 21, 23}; - Partial Initialization:
int num[5] = {10, 20};(Remaining elements 2, 3, and 4 are automatically set to 0). - Initialization without Size:
int score[] = {10, 20, 30};(Compiler automatically sets the size to 3).
4. Accessing Array Elements
Array elements are accessed using an index (also called a subscript). In C, array indexing always starts from 0 and ends at size - 1.
int marks[3] = {85, 90, 78}; printf("%d", marks[0]); // Output: 85 printf("%d", marks[2]); // Output: 78 5. Memory Representation of Arrays
Since arrays occupy contiguous memory, the address of any element can be calculated if the base address (address of the first element) is known.
Formula for address calculation:
Address of A[i] = Base Address + (i * size_of_data_type)
Example: If an integer array starts at address 1000 and each integer takes 4 bytes, then marks[2] would be at:
1000 + (2 * 4) = 1008.
6. Multidimensional Arrays (2D Arrays)
A 2D array is essentially an "array of arrays." It is commonly used to represent tables or matrices.
Syntax:
data_type array_name[rows][columns];
Example: int matrix[2][3] = { {1, 2, 3}, {4, 5, 6} };
This creates a table with 2 rows and 3 columns.
7. Array-Related Functions and Operations
Common operations performed on arrays include:
- Traversal: Visiting each element of the array exactly once (usually using a
forloop). - Insertion: Adding a new element at a specific index.
- Deletion: Removing an element from a specific index.
- Searching: Finding the location of a specific element (e.g., Linear Search).
8. Exam Focus Enhancements
Exam Tips
- Zero Indexing: Never forget that the first element is
array[0], notarray[1]. - Looping: When traversing an array of size N, your loop condition should be
i < Nori <= N-1. - 2D Arrays: In a 2D array, the first index always represents the Row and the second represents the Column.
Common Mistakes
- Array Out of Bounds: Accessing
marks[5]in an array of size 5. C does not check for boundary errors; it will simply access "garbage" memory, which can crash your program. - Inconsistent Types: Trying to store a
floatin anintarray. This will lead to data loss (truncation). - Forgetting Size: You cannot declare an array as
int a[];without immediate initialization. The compiler must know the size to allocate memory.
Frequently Asked Questions
Q: Why is it called a "Static" data structure?
A: Because the size of the array is fixed at compile-time and cannot be changed during the execution of the program.
Q: What is the base address?
A: The base address is the memory location of the very first element (index 0) of the array.