Pointers in C can often be difficult to understand—I certainly had a learning curve and am continuing to learn. However, I had a thought that may help some by comparing a common experience and wanted to share.

A pointer in C behaves just like a word in any spoken language which refers to a physical object or multiple objects and the uniqueness of each object (e.g Skippy the dog, Mittens and Tiger the cats, fork number 5). The word itself does not contain the physical object and its uniqueness but only communicates the existence of the physical object and its uniqueness. The pointer itself does not contain the physical address and its value but only communicates the existence of the physical address and its value.

  • deur@feddit.nl
    link
    fedilink
    arrow-up
    12
    ·
    edit-2
    6 days ago

    The better analogy is that people live in houses and houses have addresses, and I can use an address to find someone’s house.

    Whether the pointed data by the pointer is valid or not is… not the point. In all languages I can think of, dereferencing an invalid pointer like a pointer to the wrong address per the type and alignment is never valid. Your analogy does not improve on historical analogies and it is wrong.

    • Ryick@lemm.eeOP
      link
      fedilink
      arrow-up
      1
      ·
      edit-2
      5 days ago

      To be a bit more specific, a word is nothing more than a set of symbols (physical) which indicate an agreed value (abstract/reference). A pointer is the word and the agreed value is the reference of the object. The object the pointer points to is the existence of the real object (physical) and its value(s) which exists regardless of abstract references.

      Pattern: physical -> reference -> physical -> value

      Double pointer pattern: physical -> reference -> physical -> reference -> physical -> value

      Etc…

      A word’s meaning can change through time as cultures rise and fall, for the temporary purpose of encrypting conversations (e.g. the word “dog” can point to the agreed value of “fork” or “7”), or even misidentification.

    • Ryick@lemm.eeOP
      link
      fedilink
      arrow-up
      1
      ·
      5 days ago

      That is a simple and good analogy, and, yes, perhaps better than my own.

      Whether the pointed data by the pointer is valid or not is… not the point.

      Nor is that the point of my analogy, but I do see how you inferred that point. Your criticism has helped me identify a flaw in how I express the analogy.