On this page
Pointers & Pointer Arithmetic
Prev: pointers_and_memory | Up: pointers_and_memory | Next: Dynamic Memory & Allocators
Rel: Pointers, References & Indirection, Arrays & C Strings
Ref: cppreference — Pointer Declaration
Tags: #public #prog #c #pointers #memory
Contents
- > Address-Of, Dereference & Scaled Arithmetic
- > Reading `const` Pointer Declarations (Right-to-Left)
- > `void *` & Double Pointers (`T **`)
Address-Of, Dereference & Scaled Arithmetic
Adding 1 to T *p advances the address by sizeof(T) bytes, not 1 byte. Indexing p[i] is defined as *(p + i).
int nums[3] = {10, 20, 30};
int *p = nums;
int second = *(p + 1); // 20 (advances by sizeof(int) bytes)
Reading const Pointer Declarations (Right-to-Left)
const int *p— pointer to aconst int(cannot modify*p, can reassignp).int * const p—constpointer toint(can modify*p, cannot reassignp).const int * const p— neitherpnor*pcan be modified.
void * & Double Pointers (T **)
void *is a generic untyped pointer (used bymalloc,memcpy,qsort); cast to a concreteT *before dereferencing or doing pointer arithmetic.T **(pointer to pointer) lets a function rebind a caller's pointer (e.g.,out-parameters or resetting freed pointers toNULL).
Linked from
Related notes
- tuts/c/pointers_and_memory Linked here; links to this page; shared tags: #c, #memory, #pointers, #prog
- tuts/c/pointers_and_memory/dynamic_memory_and_allocators Linked here; links to this page; shared tags: #c, #memory, #prog
- tuts/programming/memory_and_execution/pointers_references_and_indirection Linked here; links to this page; shared tags: #memory, #pointers, #prog
- tuts/c/fundamentals/arrays_and_strings Linked here; links to this page; shared tags: #c, #prog
- tuts/c/builtins/const links to this page; shared tags: #c, #prog
- tuts/c/builtins/void links to this page; shared tags: #c, #prog
- tuts/c/compilation_and_internals/memory_layout_and_abi shared tags: #c, #memory, #prog
- tuts/c/pointers_and_memory/function_pointers_and_callbacks shared tags: #c, #pointers, #prog
- tuts/c/pointers_and_memory/undefined_behavior_and_sanitizers shared tags: #c, #memory, #prog
- tuts/c/tutorials/arena_allocator_pattern shared tags: #c, #memory, #prog