Pointers and Arrays
In C, arrays and pointers are closely related. An array name is treated as a pointer to the first element, and you can move to the address of the next element by adding an integer to a pointer (pointer arithmetic).
Syntax
// An array name can be used directly as a pointer to the first element.
int arr[5] = {10, 20, 30, 40, 50};
int *p = arr; // Same as &arr[0].
// Index notation and pointer notation are equivalent.
arr[i] // Index notation
*(arr + i) // Pointer notation
// Incrementing a pointer advances to the next element.
p++; // Advances the address by sizeof(int) bytes.
p += n; // Advances the address by n elements.
// Passing an array to a function via a pointer.
void func(int *arr, int size);
void func(int arr[], int size); // Same meaning as above.
Pointer and Array Correspondence
| Notation | Equivalent | Description |
|---|---|---|
| arr | &arr[0] | The address of the first element of the array. |
| *arr | arr[0] | The value of the first element of the array. |
| arr + i | &arr[i] | The address of the i-th element. |
| *(arr + i) | arr[i] | The value of the i-th element. |
| p++ | Move to next element | Increases the address by the size of the type. |
| p2 - p1 | Difference in elements | Returns how many elements apart the two pointers are. |
Sample Code
#include <stdio.h>
// A function that receives an array as a pointer and returns the sum.
int sum(int *arr, int size) {
int total = 0;
for (int i = 0; i < size; i++) {
total += arr[i]; // Same as *(arr + i).
}
return total;
}
// An alternative way to loop using pointer arithmetic.
void print_array(const int *p, int size) {
const int *end = p + size; // One past the last element.
while (p < end) {
printf("%d ", *p);
p++; // Advance to the next element.
}
printf("\n");
}
int main(void) {
int scores[] = {80, 75, 90, 65, 88};
int n = sizeof(scores) / sizeof(scores[0]);
// Index notation and pointer notation are equivalent.
printf("scores[2] = %d\n", scores[2]); // Prints "90".
printf("*(scores+2) = %d\n", *(scores+2)); // Prints "90".
// Using a pointer variable to work with the array.
int *p = scores;
printf("*p = %d\n", *p); // First element: prints "80".
p++;
printf("*p = %d\n", *p); // Next element: prints "75".
// Passing the array to functions.
printf("Sum: %d\n", sum(scores, n)); // Prints "Sum: 398".
print_array(scores, n); // Prints "80 75 90 65 88".
// Relationship between 2D arrays and pointers.
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
// matrix[i][j] is the same as *(*(matrix + i) + j).
printf("matrix[1][2] = %d\n", *(*(matrix + 1) + 2)); // Prints "6".
return 0;
}
Notes
Although an array name can be used like a pointer, there is a fundamental difference. An array name is itself an address and cannot be assigned a different address. A pointer variable, on the other hand, can hold any address and supports incrementing and reassignment.
When you pass an array to a function, size information is lost. Using sizeof(arr) inside the function returns the size of the pointer, not the size of the entire array. Always pass the array size as a separate argument.
For the basics of pointers, see Pointer Basics. The relationship between strings (char arrays) and pointers is covered in Pointers and Strings.
If you find any errors or copyright issues, please contact us.