📘 Arrays in C
1. What is an Array?
An array is a collection of elements of the same data type stored in contiguous memory locations.
Instead of declaring many variables, we can group them together.
👉 Example:
Instead of writing
int marks1, marks2, marks3, marks4, marks5;
We can write
int marks[5];
2. Declaration of Array
data_type array_name[size];
✅ Example:
int numbers[10]; // array of 10 integers
float prices[5]; // array of 5 floats
char grade[3]; // array of 3 characters
3. Initialization of Array
a) Compile-time Initialization
int arr[5] = {10, 20, 30, 40, 50};
b) Partial Initialization
int arr[5] = {10, 20}; // Remaining will be 0 → {10, 20, 0, 0, 0}
c) Without size
int arr[] = {1, 2, 3, 4}; // Size = 4 (auto-calculated)
4. Accessing Array Elements
Array index starts from 0.
arr[0]→ first elementarr[1]→ second element
👉 Example:
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
printf("First element = %d\n", arr[0]);
printf("Third element = %d\n", arr[2]);
printf("Last element = %d\n", arr[4]);
return 0;
}
✅ Output:
First element = 10
Third element = 30
Last element = 50
5. Input & Output using Arrays
👉 Example:
#include <stdio.h>
int main() {
int marks[5];
int i;
printf("Enter 5 marks:\n");
for(i = 0; i < 5; i++) {
scanf("%d", &marks[i]);
}
printf("\nYou entered:\n");
for(i = 0; i < 5; i++) {
printf("%d ", marks[i]);
}
return 0;
}
6. Memory Representation
int arr[5] = {10, 20, 30, 40, 50};
Index: 0 1 2 3 4
Value: 10 20 30 40 50
Stored in contiguous memory.
If base address = 1000, and
int= 4 bytes:
arr[0] → 1000
arr[1] → 1004
arr[2] → 1008
...
📘 Arrays and Pointers in C
1. Relationship between Arrays and Pointers
In C, the name of the array is a pointer to its first element.
Example:
int arr[5] = {10, 20, 30, 40, 50}; printf("%p\n", arr); // address of arr[0] printf("%p\n", &arr[0]); // also address of arr[0]
✅ Both will print the same address.
2. Accessing Array Elements using Pointers
👉 Normal way:
arr[2]; // third element
👉 Pointer way:
*(arr + 2); // third element
💡 Rule:arr[i] == *(arr + i)
3. Example Program
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int *ptr = arr; // pointer points to first element
printf("Using array indexing:\n");
for(int i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
printf("\nUsing pointer arithmetic:\n");
for(int i = 0; i < 5; i++) {
printf("%d ", *(ptr + i));
}
return 0;
}
✅ Output:
Using array indexing:
10 20 30 40 50
Using pointer arithmetic:
10 20 30 40 50
4. Pointer Arithmetic with Arrays
If ptr is pointing to arr[0]:
ptr + 1→ address ofarr[1]ptr + 2→ address ofarr[2]*(ptr + 2)→ value atarr[2]
👉 Example:
#include <stdio.h>
int main() {
int arr[3] = {5, 10, 15};
int *ptr = arr;
printf("Value at arr[0] = %d\n", *ptr);
printf("Value at arr[1] = %d\n", *(ptr + 1));
printf("Value at arr[2] = %d\n", *(ptr + 2));
return 0;
}
✅ Output:
Value at arr[0] = 5
Value at arr[1] = 10
Value at arr[2] = 15
5. Arrays of Pointers
You can also create an array of pointers.
👉 Example:
#include <stdio.h>
int main() {
const char *names[3] = {"Amit", "Rahul", "Sneha"};
for(int i = 0; i < 3; i++) {
printf("%s\n", names[i]);
}
return 0;
}
✅ Output:
Amit
Rahul
Sneha