Arrays and Pointers in C
Arrays and pointers are tightly connected in C.
If you truly understand this lesson, you will understand how C works internally.
Many students fear pointers only because they do not understand how arrays and pointers relate to each other.
Let us clear that confusion step by step.
Important Truth
In C:
The name of an array behaves like a pointer to its first element.
This does not mean arrays are pointers, but they can be used like pointers in many situations.
Array Memory Layout
Consider this array:
int arr[5] = {10, 20, 30, 40, 50};
In memory, elements are stored in continuous locations:
arr[0]→ first memory blockarr[1]→ next memory block- and so on…
The array name arr holds the address of
the first element.
Array Name as Pointer
These two expressions point to the same memory location:
arr
&arr[0]
That is why this works:
printf("%d", *arr);
It prints the first element of the array.
Accessing Array Elements Using Pointers
Let us access all elements using pointer arithmetic.
#include <stdio.h>
int main() {
int arr[5] = {10, 20, 30, 40, 50};
int i;
for (i = 0; i < 5; i++) {
printf("%d ", *(arr + i));
}
return 0;
}
Explanation:
arr→ points to first elementarr + i→ moves to ith element*(arr + i)→ value at that location
Using Pointer Variable with Array
We can also use a pointer variable.
int arr[3] = {5, 10, 15};
int *ptr = arr;
printf("%d\n", *ptr); // 5
printf("%d\n", *(ptr+1)); // 10
printf("%d\n", *(ptr+2)); // 15
Both approaches are valid and commonly used.
Important Difference: Array vs Pointer
Even though arrays act like pointers, they are NOT the same.
- Array size is fixed
- Array name cannot be reassigned
- Pointer can be reassigned
Example (invalid):
arr = ptr; // ❌ Not allowed
Why This Matters
Understanding arrays and pointers is essential for:
- Dynamic memory allocation
- Function arguments
- Strings
- Data structures
Almost everything advanced in C builds on this concept.
Common Mistakes
- Accessing memory outside array bounds
- Confusing
arrand&arr - Forgetting pointer arithmetic rules
Mini Practice
- Print array elements using pointer only
- Print addresses of each element
- Modify array values using pointer
Quick Quiz
Q1. What does the array name represent?
It represents the address of the first element.
Q2. What is *(arr + i)?
It accesses the ith element of the array.
Q3. Can array names be reassigned?
No, array names cannot be reassigned.
Q4. Why are arrays stored contiguously?
For efficient access using pointer arithmetic.
Q5. Why is this concept important?
It is the foundation for memory handling and data structures.