liveBook · Manning

Pointer comparison

As explained in Modern C, Third Edition

Pointer comparison is an operation that evaluates an ordering or equality relationship between pointer values.

Pointer comparison is constrained by the array object containing the pointed-to elements. Relational operators such as <, <=, >=, and > are valid only when both pointers point into the same array; otherwise, the operation itself can be erroneous. section 15.1.1

TAKEAWAY:

  • Do not treat pointers as unrestricted integers: address-space representation can impose stricter comparison rules. section 15.1.1
  • Keep pointer comparisons within one array object, just as pointer subtraction must remain within one array. section 15.1.1
  • Prefer array indexing when expressing access to a specific element; compilers may detect invalid indices more readily than explicit pointer arithmetic. section 15.1.1

How Modern C, Third Edition covers it

How deep it goes
Explains same-array validity and representation-dependent restrictions, without developing a broader pointer-comparison implementation model.
Examples
Uses relational operators and recycled target buffers to illustrate valid and erroneous comparisons.
What you can do after
Evaluate whether a pointer comparison stays within one array object and choose indexing when expressing element access.
How it connects
Connects pointer comparison with pointer arithmetic, pointer subtraction, array objects, array indexing, and undefined operations.

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