Pointer Arithmetic - MarekBykowski/readme GitHub Wiki

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!