C Pointers & Arrays
Pointers and arrays are closely related in C. In most expressions, an array name automatically turns into a pointer to its first element — this is called array decay. Understanding the connection explains how strings work, how arrays are passed to functions, and what the [] operator really does.
An array name is (almost) a pointer
#include <stdio.h>
int main(void) {
int nums[5] = {10, 20, 30, 40, 50};
int *p = nums; // same as: int *p = &nums[0];
printf("nums[0] = %d, *p = %d\n", nums[0], *p);
printf("nums[2] = %d, *(p + 2) = %d, p[2] = %d\n", nums[2], *(p + 2), p[2]);
printf("*(nums + 4) = %d\n", *(nums + 4));
return 0;
}nums[0] = 10, *p = 10
nums[2] = 30, *(p + 2) = 30, p[2] = 30
*(nums + 4) = 50
In fact, C defines the subscript operator in terms of pointers: a[i] means exactly *(a + i). That works for both arrays and pointers.
Pointer arithmetic
Adding an integer to a pointer moves it by that many elements, not bytes. The compiler multiplies by the element size for you. For an int *, p + 1 is 4 bytes further; for a double *, 8 bytes further.
#include <stdio.h>
int main(void) {
double prices[4] = {1.5, 2.25, 3.0, 4.75};
double *start = prices;
double *end = prices + 4; // one past the last element
double total = 0;
for (double *p = start; p < end; p++) {
total += *p;
}
printf("total = %.2f\n", total);
printf("elements between start and end: %td\n", end - start);
printf("bytes between them: %td\n", (char *) end - (char *) start);
return 0;
}total = 11.50
elements between start and end: 4
bytes between them: 32
| Operation | Meaning |
|---|---|
p + n, p - n |
pointer moved by n elements |
p++, p-- |
move to the next / previous element |
q - p |
number of elements between two pointers into the same array |
p < q, p == q |
compare positions within the same array |
A pointer may point one element past the end of an array (like end above) for comparisons, but it must never be dereferenced there.
Where arrays and pointers differ
Decay makes them feel identical, but they are not:
#include <stdio.h>
int main(void) {
int arr[6] = {0};
int *ptr = arr;
printf("sizeof(arr) = %zu\n", sizeof(arr)); // whole array
printf("sizeof(ptr) = %zu\n", sizeof(ptr)); // just an address
ptr++; // fine: ptr is a variable
// arr++; // error: an array cannot be moved
printf("ptr now points to index %td\n", ptr - arr);
return 0;
}sizeof(arr) = 24
sizeof(ptr) = 8
ptr now points to index 1
Passing arrays to functions
When you pass an array to a function, only the address of the first element is passed. The function cannot know the length, so you pass it separately. Because the function has the address, it can modify the caller's array:
#include <stdio.h>
void scale(int *values, int count, int factor) {
for (int i = 0; i < count; i++) {
values[i] *= factor; // same as *(values + i) *= factor
}
}
int sum(const int values[], int count) { // int values[] is the same as int *values
int s = 0;
for (int i = 0; i < count; i++) s += values[i];
return s;
}
int main(void) {
int data[] = {3, 1, 4, 1, 5};
int n = sizeof(data) / sizeof(data[0]);
scale(data, n, 10);
for (int i = 0; i < n; i++) printf("%d ", data[i]);
printf("\nsum = %d\n", sum(data, n));
return 0;
}30 10 40 10 50
sum = 140
The const in sum promises that the function only reads the array — the compiler will reject any attempt to modify it.
Strings and pointers
Since strings are char arrays, walking through them with a pointer is very common in C code. Here are two classic string routines written with pointers:
#include <stdio.h>
size_t my_strlen(const char *s) {
const char *p = s;
while (*p != '\0') {
p++;
}
return (size_t)(p - s);
}
void reverse(char *s) {
char *left = s;
char *right = s + my_strlen(s) - 1;
while (left < right) {
char t = *left;
*left++ = *right;
*right-- = t;
}
}
int main(void) {
char word[] = "pointers";
printf("length of \"%s\" = %zu\n", word, my_strlen(word));
reverse(word);
printf("reversed: %s\n", word);
return 0;
}length of "pointers" = 8
reversed: sretniop
*left++ = *right; stores into *left and then advances left — postfix ++ binds to the pointer, not to the character.
Tip: Indexing (
a[i]) and pointer arithmetic (*(a + i)) compile to the same code. Use whichever is clearer; for most loops that is indexing.
Test yourself
If int a[5]; and int *p = a;, which expression is equivalent to a[3]?
- *p + 3
- *(p + 3)
- &p[3]
- p + 3
For double *d, how many bytes further does d + 1 point (with 8-byte doubles)?
- 1
- 4
- 8
- 16
Inside void f(int arr[]), what does sizeof(arr) give?
- The array length
- The size of the whole array in bytes
- The size of a pointer
- 0