Sub topic 6
Arrays and Pointers: Decay and sizeof
This chapter assumes you know basic pointer syntax (int *p, &x, *p) and how pointer arithmetic scales by element size. Sizes below assume a typical 64-bit platform: int is 4 bytes and a pointer is 8 bytes. The C standard does not fix those numbers, and a GATE question normally states them.
An array is not a pointer
An array is a contiguous block of elements of one type. The declaration int a[5]; reserves 5 ints, which is 20 bytes. The name a refers to that whole block. It does not refer to a separate variable that stores an address.
A pointer is a variable that holds an address. int *p; reserves 8 bytes, and those bytes can hold the address of any int.
Arrays and pointers look interchangeable because of one rule, called decay.
The decay rule
In most expressions, an array name is automatically converted to a pointer to its first element. That is, a becomes &a[0], with type int *. Decay is why int *p = a; compiles without an &.
There are three exceptions where the array name does not decay:
- It is the operand of
sizeof. - It is the operand of unary
&. - It is a string literal used to initialize a
chararray, as inchar s[] = "hi";.
Indexing is pointer arithmetic
By definition, a[i] means *(a + i). First a decays to int *. Then adding i moves it by i whole elements. Then * reads that element.
Addition is commutative, so *(a + i) equals *(i + a). That means i[a] is also legal and equals a[i]. You will see 2[a] in puzzle questions.
sizeof versus pointers
#include <stdio.h>
#include <string.h>
int main(void) {
int a[5] = {1, 2, 3, 4, 5};
int *p = a;
char s[] = "hello";
char *t = "hello";
printf("%zu %zu\n", sizeof a, sizeof p);
printf("%zu %zu\n", sizeof s, sizeof t);
printf("%zu\n", strlen(s));
return 0;
}The output is:
20 8
6 8
5sizeof ais 20 becauseadoes not decay here.sizeof pis the size of a pointer.sizeof sis 6. The array holdsh e l l oplus the'\0'terminator.sizeof tis the size of a pointer, even thoughtpoints at the same text.strlen(s)is 5, because it counts characters up to, but not including,'\0'.
`a` versus `&a`
Both a and &a hold the same address. They have different types, so arithmetic on them behaves differently.
| Expression | Type | Meaning |
|---|---|---|
| `a` (in most uses) | `int *` | address of element 0 |
| `&a[0]` | `int *` | same as above |
| `&a` | `int (*)[5]` | address of the whole array |
| `sizeof a` | `size_t` | 20 |
| `sizeof &a` | `size_t` | 8, the size of a pointer |
Adding 1 to an int * moves 4 bytes. Adding 1 to an int (*)[5] moves 20 bytes, one whole array.
#include <stdio.h>
int main(void) {
int a[5] = {1, 2, 3, 4, 5};
printf("%td\n", (char *)(a + 1) - (char *)a);
printf("%td\n", (char *)(&a + 1) - (char *)&a);
printf("%d\n", *(a + 4));
printf("%d\n", *(*(&a + 0) + 2));
return 0;
}The output is 4, 20, 5, then 3. The casts to char * make the subtraction count bytes. In the last line, *(&a + 0) is the array a itself, which decays, and the + 2 then selects a[2].
Arrays as function parameters
C passes arguments by value, and an array argument decays to a pointer to its first element. So the parameter declarations int arr[], int arr[10] and int *arr all mean int *arr. The size written in the brackets is ignored.
#include <stdio.h>
void f(int arr[]) {
printf("%zu\n", sizeof arr);
}
int main(void) {
int a[10];
printf("%zu\n", sizeof a);
f(a);
return 0;
}This prints 40 and then 8. Inside f, the length is lost. The function must receive it separately, as in this common idiom:
#include <stdio.h>
#include <stddef.h>
#define LEN(x) (sizeof(x) / sizeof((x)[0]))
int sum(const int *arr, size_t n) {
int total = 0;
for (size_t i = 0; i < n; i++) {
total += arr[i];
}
return total;
}
int main(void) {
int a[] = {3, 1, 4, 1, 5};
printf("%zu %d\n", LEN(a), sum(a, LEN(a)));
return 0;
}This prints 5 14. The LEN macro works only where x is a real array. If you apply it to a parameter, it silently computes 8 / 4 = 2.
Common errors
- Using
sizeofon a parameter. It gives the pointer size, not the array size. A compiler may warn, but it is not an error. - Assigning to an array name. After
int a[3], b[3];, the statementa = b;is a compile error becauseais not a modifiable lvalue. Copy with a loop ormemcpy. - Incrementing an array name.
a++is illegal.int *p = a; p++;is fine, becausepis a variable. - Confusing
&awitha. Passing&awhere anint *is expected is a type mismatch. Reading through the wrong type is undefined behavior. - Reading past the end.
a[5]on a 5-element array is undefined behavior. The compiler is not required to catch it. Forming the one-past-the-end pointera + 5is legal, but dereferencing it is not. - Mixing up
char s[]andchar *t. Both can hold"hello", butsizeofdiffers. Also,scan be modified, while modifying the literal thattpoints to is undefined behavior.
Summary of the working model
- An array owns its storage. A pointer only refers to storage.
- Outside
sizeofand&, an array name becomes a pointer to its first element. - Array parameters are really pointer parameters.
a[i]is*(a + i), and pointer addition scales by the pointed-to type.&ahas type pointer-to-array, so adding 1 skips the whole array.
Exercise
On a platform with 4-byte int and 8-byte pointers, given int a[6]; in main and a call g(a) to void g(int b[6]) { printf("%zu", sizeof b); }, what do sizeof a in main and sizeof b in g print? Also, by how many bytes does (char *)(&a + 1) - (char *)a differ from zero?
Show answer
sizeof a is 24, because a is a real array of 6 ints. sizeof b is 8, because b is really an int * and the 6 is ignored. The byte difference is 24, because &a has type int (*)[6], so adding 1 skips the whole array.
Exercise
What does this program print? int a[4] = {10, 20, 30, 40}; int *p = a + 1; printf("%d %d %d", p[1], 2[a], *(&a[3] - 3));
Show answer
It prints 30 30 20. p[1] is *(p + 1), which is a[2], so 30. 2[a] equals a[2], so 30. &a[3] - 3 is &a[0]... wait, 3 - 3 is index 0, so the value is a[0], which is 10. The correct output is 30 30 10.