this post was submitted on 18 May 2025
0 points (50.0% liked)

Programming

20211 readers
191 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



founded 2 years ago
MODERATORS
 

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.

top 12 comments
sorted by: hot top controversial new old
[–] Corbin@programming.dev 3 points 10 hours ago

It's not even an analogy; pointers and reference mechanics are the same concept in programming and linguistics. See the page on referents for an example blend of viewpoints.

[–] TehPers@beehaw.org 2 points 13 hours ago (1 children)

Pronouns are pointers. "Let us (let's) move it over there." Both "us" and "it" indirectly refer to something else by a new name. Like pointers, the pointees are defined by some context external to that sentence/statement (usually earlier sentences/statements or some other actions). The meaning of "us" and "it" can change as well in different contexts, and as such, those words are not bound to one value (and "rebinding" those words by changing contexts does not change the values they were previously bound to).

[–] Ryick@lemm.ee 2 points 13 hours ago

Yes! 😁

One could even say that the pronouns “him” and “her” could indicate specific types of pointers, like int and char.

[–] deur@feddit.nl 9 points 22 hours ago* (last edited 22 hours ago) (2 children)

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.ee 1 points 18 hours ago* (last edited 18 hours 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.ee 1 points 19 hours 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.

[–] bleistift2@sopuli.xyz 2 points 20 hours ago (2 children)

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@programming.dev 1 points 19 hours ago* (last edited 19 hours 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.ee 1 points 19 hours ago

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

[–] Sunsofold@lemmings.world 1 points 18 hours ago (1 children)

I tried seeing if the analogy could be made to work but I am unaware of any natural language with an analog to pointers. Natural language references generally frame the concept that serves as a pointer in relation to the object rather than as a particular object.

[–] Ryick@lemm.ee 1 points 17 hours ago (1 children)

Let’s make an agreement that the English word “coffee” now means 65. The reference of any word within any language only exists because we make an agreement with ourselves or with others for what a particular set of symbols gives reference to. Without an agreement, all the symbols we use mean nothing more than an arbitrary scratch on a table.

A pointer is the word, which means nothing by itself other than a grouping of letters exist, and the reference to an address is the reference to an object, which exists independently from the object—a reference to a fictional object can exist and a real object can exist without a reference.

[–] Sunsofold@lemmings.world 1 points 12 hours ago

Human language has 2 layers. Signifier and signified. Computers have 3. Signifier (variable name), memory address, (no human analog) and signified. (stored value) A pointer would be a signifier (word) for which the signified is the non-existent human analog to a memory address. You can have a word that refers to a thing that doesn't exist (e.g. unicorn) but it won't help the analogy.

In your example, the signifier 'coffee' is akin to a variable name. It is a signifier that dereferences to a value within the scope of our agreement. We can agree it holds the stored value of 65, allowing us to say something like butter = coffee - 60, bring me 'butter' eggs, but we don't have a memory address system so it's not the same process as a computer.

For a computer, there is a middle step to a variable dereference. It sees 'coffee' and first looks at the table of references, finds the variable 'coffee' has it's value stored at address 1 ['coffee':1, 'butter':88, 'unicorn':73] and then pulls the value (65) from the memory at address 1. A pointer would be another variable, such as '*monkey' (e.g. *monkey = &coffee, which can be read as "pointer monkey shall hold the value of the address at which coffee is stored") which would be added into the reference table as a variable stored at another address, say address 2, and would store in address 2 a value of (1).

word        address       value
coffee      1                65
*monkey     2                1

Language just doesn't have the analogous concept.