Array in C language
Array in C Language
Arrays are one of the most important and frequently used data structures in the C programming language. They allow us to store multiple values of the same data type in a single variable. Understanding arrays is essential for mastering C because they form the foundation for advanced topics like strings, pointers, structures, and dynamic memory allocation.
1. Introduction to Arrays
An array is a collection of elements of the same data type stored in contiguous memory locations. Instead of declaring many variables separately, we can use an array to store similar values.
Example without array:
int a1 = 10;
int a2 = 20;
int a3 = 30;
Example with array:
int a[3] = {10, 20, 30};
Here:
ais the array name.3is the size.- Elements are accessed using index numbers (starting from 0).
2. Declaration of Array
Syntax:
data_type array_name[size];
Example:
int marks[5];
This declares an array named marks that can store 5 integers.
3. Initialization of Array
Arrays can be initialized at the time of declaration.
Method 1: Full Initialization
int arr[5] = {1, 2, 3, 4, 5};
Method 2: Partial Initialization
int arr[5] = {1, 2};
Remaining elements become 0.
Method 3: Without Size
int arr[] = {10, 20, 30};
Compiler automatically counts the size (3).
4. Accessing Array Elements
Array elements are accessed using index numbers.
- Index starts from 0
- Last index = size - 1
int arr[3] = {10, 20, 30};
printf("%d", arr[0]); // Output: 10
printf("%d", arr[2]); // Output: 30
5. Memory Representation of Array
Arrays store elements in contiguous memory locations.
If:
int arr[3] = {10, 20, 30};
Memory layout (assuming 4 bytes per int):
| Index | Value | Address Example |
|---|---|---|
| 0 | 10 | 1000 |
| 1 | 20 | 1004 |
| 2 | 30 | 1008 |
Formula:
Address = Base Address + (Index × Size of Data Type)
6. Input and Output in Array
Taking Input:
int arr[5];
for(int i = 0; i < 5; i++) {
scanf("%d", &arr[i]);
}
Printing Output:
for(int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
7. Types of Arrays in C
1. One-Dimensional Array
Simple linear array.
int arr[5];
2. Two-Dimensional Array
Used for matrices (rows and columns).
int matrix[3][3];
Accessing:
matrix[0][1];
Initialization:
int matrix[2][2] = {
{1, 2},
{3, 4}
};
3. Multi-Dimensional Array
More than two dimensions.
int arr[2][3][4];
8. Strings as Arrays
In C, strings are arrays of characters ending with a null character \0.
char name[6] = "Hello";
Memory: H e l l o \0
Example:
printf("%s", name);
9. Passing Array to Function
Arrays are passed by reference (actually by address).
void display(int arr[], int size) {
for(int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
}
int main() {
int arr[3] = {1, 2, 3};
display(arr, 3);
}
Important:
- Changes inside function affect original array.
10. Array and Pointers
Array name represents the base address.
int arr[3] = {10, 20, 30};
arr is equivalent to &arr[0].
Pointer example:
int *ptr = arr;
printf("%d", *(ptr + 1)); // Output: 20
Relationship:
arr[i] == *(arr + i)
11. Dynamic Array in C
Static arrays have fixed size. For flexible size, use dynamic memory allocation.
Using malloc():
#include <stdlib.h>
int *arr;
arr = (int*) malloc(5 * sizeof(int));
Free memory:
free(arr);
12. Common Array Operations
1. Traversing
Accessing all elements.
2. Searching
Linear Search:
for(int i = 0; i < n; i++) {
if(arr[i] == key)
printf("Found");
}
Binary Search (Sorted Array Required)
3. Sorting
Bubble Sort Example:
for(int i = 0; i < n-1; i++) {
for(int j = 0; j < n-i-1; j++) {
if(arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
13. Advantages of Arrays
- Easy to store multiple values.
- Random access using index.
- Efficient memory usage.
- Useful for implementing other data structures like stacks, queues.
14. Disadvantages of Arrays
- Fixed size (in static arrays).
- Insertion and deletion are costly.
- Cannot store different data types.
- Risk of out-of-bounds error.
15. Applications of Arrays
- Storing marks of students.
- Matrix operations.
- String handling.
- Searching and sorting algorithms.
- Implementing stacks and queues.
- Image processing.
- Statistical calculations.
16. Important Concepts Related to Arrays
1. Array Index Out of Bounds
Accessing invalid index causes undefined behavior.
int arr[3];
arr[5] = 10; // Dangerous
2. Array Size Determination
int size = sizeof(arr) / sizeof(arr[0]);
3. Const Array
const int arr[3] = {1,2,3};
Values cannot be changed.
17. Difference Between Array and Pointer
| Array | Pointer |
|---|---|
| Fixed memory | Dynamic memory |
| Size fixed | Can change location |
| arr++ not allowed | ptr++ allowed |
18. Example Program (Complete)
#include <stdio.h>
int main() {
int n, sum = 0;
printf("Enter number of elements: ");
scanf("%d", &n);
int arr[n];
for(int i = 0; i < n; i++) {
scanf("%d", &arr[i]);
sum += arr[i];
}
printf("Sum = %d", sum);
return 0;
}
19. Multidimensional Array Memory Formula
For 2D array:
Address = Base + [(Row × Total Columns) + Column] × SizeConclusion
Arrays are a fundamental concept in C programming. They provide a structured way to store and manipulate multiple data elements efficiently. Understanding array declaration, initialization, memory representation, pointer relationship, and operations like searching and sorting is essential for mastering C.
Comments
Post a Comment