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.

  • bleistift2@sopuli.xyz
    link
    fedilink
    English
    arrow-up
    3
    ·
    3 days ago

    The pointer itself does not contain the physical address and its value but only communicates the existence of the physical address and its value.

    So you’re telling me 0xDEADBEEF does not contain the physical address of the thing it points to?

    • sus
      link
      fedilink
      arrow-up
      1
      ·
      edit-2
      3 days ago

      It’s a technicality about the pointer type. You can cast the type away which typically doesn’t change the actual value (but I’m pretty sure that causes undefined behavior)

      For your example, int x = 0xDEADBEEF; signifies the integer -559038737 (at least on x86.)

      char *p = (char*)0xDEADBEEF; on the other hand may or may not point to the real memory address 0xDEADBEEF, depending on factors like if the processor is using virtual or real addressing, etc

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

      Yes and no, according to the analogy. Just as a word contains the idea of an object, but not the object itself.