Pointer arithmetic in C++: why p + 1 moves 4 bytes, not 1
Pointer arithmetic in C++ moves an address by n times the size of the type. One real run: int* p + 1 moves 4 bytes, char* c + 1 moves 1.
Pointer arithmetic in C++ is adding an integer to a pointer (or subtracting one). It does not add that many bytes to the address. p + n moves the address by n * sizeof(*p) bytes: the size of the type the pointer points to. So for an int*, p + 1 is 4 bytes further on, and for a char*, c + 1 is 1 byte further on. This walks through one real run of a short program that prints the addresses and shows it happen.
Everything below comes from one run of the program at the end, compiled with g++ -std=c++20 -Wall -O2 (GCC 13) on x86-64 Linux, where int is 4 bytes. Stack addresses change on every run, so yours will differ; the distances between them will not.
What does an int array look like in memory?
int a[3] = {10, 20, 30}; is 3 × 4 = 12 bytes, one int straight after the other with no gaps. 10 is 0x0a, 20 is 0x14, 30 is 0x1e, and x86-64 stores the lowest byte first, so the bytes are:
![A hex dump of int a[3] = {10, 20, 30}: twelve bytes, 0a 00 00 00, 14 00 00 00, 1e 00 00 00, labelled a[0], a[1] and a[2]](https://vthvqgqmgbnywvxzqlws.supabase.co/storage/v1/object/public/blog-media/shorts/cpp-015-pointer-arithmetic/array.jpg)
int* p = a; stores the address of the array's first byte in p. On this run the program prints it as 0x7fff050a4888.

Why does p + 1 move 4 bytes?
Because p is an int*, p + 1 means "one int further on", not "one byte further on". The compiler multiplies by sizeof(int), which is 4. The program prints p + 1 as 0x7fff050a488c: the last byte of the address goes from 88 to 8c, which is 4 more.
*(p + 1) then reads 4 bytes at that address as an int: 14 00 00 00, which is 20, the second element.

What does a[2] actually mean?
p + 2 is 2 × 4 = 8 bytes on, 0x7fff050a4890, and *(p + 2) reads 30. Array indexing is defined as exactly this: a[2] means *(a + 2). The array name a turns into a pointer to its first element, 2 is added (scaled by 4), and the result is dereferenced. The program prints a[2] as 30, the same as *(p + 2).
![p + 2 holds 0x7fff050a4890, eight bytes on; *(p + 2) reads 30, and a[2] means *(a + 2)](https://vthvqgqmgbnywvxzqlws.supabase.co/storage/v1/object/public/blog-media/shorts/cpp-015-pointer-arithmetic/p-plus-2.jpg)
Why does a char* step only 1 byte?
char* c = (char*)a; holds the same address as p, but its type is different. sizeof(char) is 1, so c + 1 is just 1 byte on: the program prints 0x7fff050a4889. Same start address, different step, because the step is the size of the pointed-to type.

| pointer | type | + 1 moves |
address on this run |
|---|---|---|---|
p |
int* |
— | 0x7fff050a4888 |
p + 1 |
int* |
4 bytes | 0x7fff050a488c |
p + 2 |
int* |
8 bytes | 0x7fff050a4890 |
c + 1 |
char* |
1 byte | 0x7fff050a4889 |
Pointer arithmetic is only defined inside one array (and one past its end). p + 3 is allowed as an end marker, but reading *(p + 3) or computing p + 4 is undefined behaviour.
The program and its output
#include <cstdio>
int main() {
int a[3] = {10, 20, 30};
int* p = a;
printf("p %p\n", (void*)p);
printf("p + 1 %p\n", (void*)(p + 1));
printf("*(p+1) %d\n", *(p + 1));
printf("*(p+2) %d\n", *(p + 2));
printf("a[2] %d\n", a[2]);
char* c = (char*)a;
printf("c + 1 %p\n", (void*)(c + 1));
}
p 0x7fff050a4888
p + 1 0x7fff050a488c
*(p+1) 20
*(p+2) 30
a[2] 30
c + 1 0x7fff050a4889
The bytes of a (0a 00 00 00 14 00 00 00 1e 00 00 00), the 4- and 8-byte steps of p + 1 and p + 2, the 1-byte step of c + 1, and a[2] == *(a + 2) were checked with a second program that copies the array's bytes out with std::memcpy and measures the distances in bytes.
Try it
If p were a double*, how many bytes would p + 1 move? Work it out from sizeof(double), then change the array to double a[3] and the pointer to double* p, print p and p + 1, and subtract the addresses.