Pointers and arrays are closely related in C.
If you already understand the basics of pointers and arrays, this topic becomes much easier. The key idea is that the name of an array can be used as an address of its first element in many expressions.
Array and Its Address
Consider:
int numbers[3] = {10, 20, 30};
The array elements are stored next to each other in memory.
Index: 0 1 2
┌─────┬─────┬─────┐
numbers → │ 10 │ 20 │ 30 │
└─────┴─────┴─────┘
The expression:
numbers
generally represents the address of the first element.
So:
numbers
is equivalent to:
&numbers[0]
in most expressions.
Accessing Array Elements Using a Pointer
We can create a pointer to the first element:
int *ptr = numbers;
Now we can access the elements using the pointer:
printf("%d", *ptr);
Output:
10
To access the next element:
printf("%d", *(ptr + 1));
Output:
20
And:
printf("%d", *(ptr + 2));
Output:
30
So:
*ptr → 10
*(ptr + 1) → 20
*(ptr + 2) → 30
Array Indexing and Pointers
One useful fact in C is that:
numbers[i]
is equivalent to:
*(numbers + i)
For example:
printf("%d", numbers[1]);
and:
printf("%d", *(numbers + 1));
both produce:
20
This is why pointers and arrays are so closely connected.
Using a Pointer with a Loop
We can use a pointer to traverse an array:
int numbers[3] = {10, 20, 30};
int *ptr = numbers;
for (int i = 0; i < 3; i++)
{
printf("%d\n", *(ptr + i));
}
Output:
10
20
30
Important Point
Although array names and pointers are closely related, an array is not itself a pointer.
For example:
int numbers[3];
int *ptr = numbers;
Here, numbers is an array, while ptr is a pointer.
They behave similarly in many expressions, but they are different types of objects.
In Simple Words
An array stores multiple values, while a pointer can be used to access those values through their memory addresses.
Remember these two relationships:
numbers → address of first element
numbers[i] → *(numbers + i)
Understanding this relationship is very important because it forms the foundation for pointer arithmetic, strings, function arguments, and dynamic memory in C.