C Pointers and Arrays
C pointers and arrays are closely connected because an array name can be used as the address of its first element. This lets pointer expressions read, change, and move through array elements.
This does not mean an array variable is exactly the same thing as a pointer variable. An array owns a fixed block of elements, while a pointer stores an address that can be changed.
Array Names Point To The First Element
In many expressions, an array name becomes a pointer to its first element. For an array named numbers, the expression numbers gives the same address as &numbers[0].
#include <stdio.h>
int main(void)
{
int numbers[3] = {10, 20, 30};
int *ptr = numbers;
printf("First with array: %d\n", numbers[0]);
printf("First with pointer: %d\n", *ptr);
printf("Same start: %s\n", ptr == &numbers[0] ? "yes" : "no");
return 0;
}
Output:
First with array: 10
First with pointer: 10
Same start: yes
The assignment int *ptr = numbers; stores the address of numbers[0] in ptr. Dereferencing ptr with *ptr reads the first array element.
Pointer Arithmetic Moves By Elements
When you add 1 to an int *, the pointer moves to the next int, not just the next byte. C uses the pointer type to know how far to move.
Because of this rule, numbers[i] and *(numbers + i) access the same element.
#include <stdio.h>
int main(void)
{
int numbers[4] = {5, 10, 15, 20};
int i;
for (i = 0; i < 4; i++) {
printf("Index %d: %d %d\n", i, numbers[i], *(numbers + i));
}
return 0;
}
Output:
Index 0: 5 5
Index 1: 10 10
Index 2: 15 15
Index 3: 20 20
The expression numbers + i points to the element at index i. The outer * then dereferences that address to get the value.
Use A Pointer To Walk Through An Array
A pointer variable can be advanced through an array one element at a time. This is useful for understanding how loops and array addresses work together.
#include <stdio.h>
int main(void)
{
int values[3] = {2, 4, 6};
int *current = values;
int i;
for (i = 0; i < 3; i++) {
printf("Value: %d\n", *current);
current++;
}
return 0;
}
Output:
Value: 2
Value: 4
Value: 6
After each print, current++ moves the pointer to the next int. The loop stops before moving outside the valid array elements.
Arrays Passed To Functions
When you pass an array to a function, the function receives a pointer to the first element. The function does not automatically know the array length, so pass the length as a separate argument.
#include <stdio.h>
void print_scores(int scores[], int length)
{
int i;
for (i = 0; i < length; i++) {
printf("Score %d: %d\n", i + 1, scores[i]);
}
}
int main(void)
{
int scores[3] = {88, 91, 79};
print_scores(scores, 3);
return 0;
}
Output:
Score 1: 88
Score 2: 91
Score 3: 79
The parameter int scores[] is treated like int *scores inside the function. Using square brackets in the parameter list is common because it shows that the function expects array data.
Important Rules
arrayoften becomes&array[0]in expressions.array[i]is equivalent to*(array + i).- Pointer arithmetic moves by elements of the pointed-to type.
- A pointer can move to another element, but an array name cannot be assigned a new address.
- Functions that receive arrays usually also need a length parameter.
- Do not read or write past the end of an array.
The key idea is that arrays provide a fixed sequence of elements, and pointers provide address-based access to those elements. Next, you will use these ideas when working with strings and dynamic memory.
