-
Notifications
You must be signed in to change notification settings - Fork 0
Pointer Arithmetic
MarekBykowski edited this page May 26, 2026
·
1 revision
int a[] = {1,2,3,4,5};
int *p = &a[1]; // points to a[1]
int *q = &a[4]; // points to a[4]
q - p; // → 3 (elements, not bytes!) type: ptrdiff_t
p + 2; // → &a[3], advances 2×sizeof(int) bytes in memory
// Pointer TO an array (not to an element)
int (*ap)[3] = &a; // type: int(*)[3]
(*ap)[0]; // → 1: deref first (→ array), then index
ap++; // advances sizeof(int[3]) = 12 bytes!
*ap[0] // → *(ap[0]) — [] beats * — DIFFERENT THING!| Type | 32-bit | 64-bit (Linux) |
|---|---|---|
int |
4 bytes | 4 bytes |
long |
4 bytes | 8 bytes |
| pointer | 4 bytes | 8 bytes |
ptrdiff_t |
4 bytes | 8 bytes |
uint32_t |
4 bytes | 4 bytes — always, use this in embedded! |