liveBook · Manning

Reference

As explained in Modern C

An object pointer is a pointer whose referenced entity is an object rather than a function.

Change an object through a function

Consider `void double_swap(double* p0, double* p1)`. A caller passes `&d0` and `&d1`; inside the function, `*p0` and `*p1` designate those two `double` objects. The function can exchange their values without knowing their names. The pointer arguments are passed by value, but they give the function access to the caller’s objects. section 11.1.1 section 2.4.2

An object pointer refers to an object, rather than a function. In `double* p`, `double` is the referenced type and `*` forms a pointer type. The pointer and its pointed-to object are distinct: assigning to `p` changes which address it holds; assigning to `*p` changes the designated object. section 11.0 section 11.1.1 section 11.5

TAKEAWAY: Keep the pointer’s state and the object it might designate separate.

  • A pointer can be valid, null, or indeterminate. Assigning `0` makes it null; a null pointer tests false. Initialize pointer variables rather than leaving their state uncertain. section 6.2 section 11.1.4
  • Dereferencing a null or indeterminate pointer has undefined behavior. Dereferencing also requires an object of the designated type; a pointer just past an array may be formed but does not designate an element to read. section 11.1.1 section 11.1.4
  • The pointer’s representation is platform-dependent. Work with the operations C supplies, not assumptions about its stored bits. section 6.2

Pointers and arrays cooperate, but are not the same object. `A[i]` means `*(A+i)` whether `A` is an array or a pointer. In a value context an array yields a pointer to its first element, losing information such as its length. Pass that length separately when a function needs it. section 11.3.1 section 11.1.2

For a structure pointer, `p->member` is shorthand for `(*p).member`; the member expression is not itself necessarily a pointer. An object pointer can also convert to `void*` and back to its original type without changing its value, but `void*` discards the referenced type information. section 11.2 section 12.4

Two pointers can designate the same object. This is aliasing: a write through one can affect a later read through the other. Pointer types matter here as well as at dereference; do not treat an address alone as proof that an object has the required type. section 12.3 section 11.1.4

How Modern C covers it

How deep it goes
Works through address-of, dereference, validity, array access, structure members, aliasing, and `void*` conversions without specifying pointer bit representations.
Examples
Uses a two-object swap, array summation, structure-member access, and two pointers that may designate one object.
What it assumes
Assumes familiarity with C objects, types, functions, and arrays; develops address-of and dereference where pointer-based mutation begins.
What you can do after
Build it from scratch: write pointer-taking C functions and check validity, referenced type, and array bounds before access.
How it connects
Connects object pointers to arrays, structures, call by value, aliasing, and `void*` conversions.

Also explained in

Compare how 17 books explain reference →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Modern C. The section links point to the parts of the book it draws on.